Update: See the comments to see how to do this correctly.
the UNIX grep command is very powerful, but the egrep command is even more powerful. You can grep for multiple strings in a file using the egrep command. All you have to do is use the | (or) symbol and add as many strings you want to look for. You have to make sure you escape the pipe symbol. This is especially helpful when you are wading through log files.
egrep "string 1"\|"string 2" file.log
The above command will only display the lines from file.log that have a reference of “string 1″ and “string 2″
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.







Hi,
I thought I would clarify that if either of those strings appear in the file it will match. If you want to do string1 AND string2, I would defer to awk like this:
$awk ‘/string1/ && /string2/ {print}’ filename
Ex:
[dordanov@maximus ~]$
[dordanov@maximus ~]$ nano
[dordanov@maximus ~]$ awk ‘/cows/ && /dogs/ {print}’ test
cows dogs
[dordanov@maximus ~]$ awk ‘/cows/ && /apples/ {print}’ test
cows apples
[dordanov@maximus ~]$ awk ‘/cows/ && /oranges/ {print}’ test
[dordanov@maximus ~]$ cat test
cows dogs
cows apples
[Reply]
srini Reply:
July 12th, 2010 at 11:04 pm
you are right. Thanks for the correction.
[Reply]