grep for words in file

Hi

Please can you help me on this:
How to grep for multiple words in a file, BUT for every word found output it to a new line.

regards

FR

So, if one line contains both 'word1' and 'word2' and you grep for both the words, you want the same line printed twice each on a new line?

Hi

No, if two words are on the same line, they can be shown on the same line, but grep finds one word on a line, it should show on a new line.
The ideia is after find the words, use mailx to mail the lines found, but on my outlook I am getting everything in one line....

regards

FR

$ grep -f pattern_filename filename

where pattern_filename look like:

pattern1
pattern2
....
....

If your input files is having a valid and contains newline character after each line, you getting probably getting the new line at destination.
It might be the client ( outlook ) or the encoding system which doesn't display it properly.

About your problem, how do you want to grep for the patterns (is it OR or AND between the patterns)?

Also, tell us the OS version on which you are working. and post the sample data and required output.

Code:
$ grep -f pattern_filename filename
where pattern_filename look like:
pattern1
pattern2
....
....
patternn

1 Like

Hi

Tarun Agrawal

Your suggestion is working fine, but what do I change to show me the last 20 lines only.

Anchal Khare:

The O.S. is hp-ux, the file is /var/adm/syslog/syslog.log

regards

FR

Pipe the output to tail -20
Something like this: grep -f pattern_filename filename | tail -20

1 Like

Hi
Balajesuri:

Thanks a lot