By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,440 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,440 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Rick Ng Chi Wah" <ng*******@netscape.net> wrote in message

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.
Nov 13 '05 #2

P: n/a
Rick Ng Chi Wah wrote:

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.
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.