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!
 
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.
 
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top