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


R

Rick Ng Chi Wah

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.

Thanks!
 
Ad

Advertisements

M

Malcolm

Rick Ng Chi Wah said:
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.
Read in each line using fgets().

The call strstr() on the line you have just read to see if it contains your
keyword.
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.
 
Ad

Advertisements

E

Ed Morton

Rick said:
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.

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

Regards,

Ed.
 

Top