delete all the entries that contain "some string" from a file

Discussion in 'C Programming' started by Rick Ng Chi Wah, Jul 28, 2003.

  1. Dear all,

    I have a text file call access.log and it contains about 5030 lines.
    Some lines contain some keyword, e.g. "ricky ng".
    How can I delete all the entries that contain "ricky ng" line and
    write to a new file call access.trace.log.
    I know we can use strstr() function.
    But, I don't know how to use strstr() function.
    Pls help me.

    Rick Ng Chi Wah, Jul 28, 2003
    1. Advertisements

  2. Rick Ng Chi Wah

    Malcolm Guest

    Read in each line using fgets().

    The call strstr() on the line you have just read to see if it contains your
    If the line contains the keyword, send it to the log.
    If the line doesn't, use fputs() to write it to a new file.

    Repeat until you come to the last line of the file.

    Swap filenames so that the new file replaces the old.
    Malcolm, Jul 28, 2003
    1. Advertisements

  3. Rick Ng Chi Wah

    Ed Morton Guest

    If this is all you're doing then don't bother writing a program, just get on
    UNIX (or Linux or download Cygwin) and run:

    grep -v "ricky ng" access.log > access.trace.log


    Ed Morton, Jul 29, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.