Categories

 

March 2010
M T W T F S S
« Feb   Apr »
1234567
891011121314
15161718192021
22232425262728
293031  

What I'm Doing...

PlaytimePriya and KunalIMG00015.jpgIMG00017.jpgFallColors.jpgSpring

Grep for multiple stuff in one file

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″

Share

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

2 comments to Grep for multiple stuff in one file

  • LinuxUser

    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

    ReplyReply

    [Reply]

    srini Reply:

    you are right. Thanks for the correction.

    ReplyReply

    [Reply]

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>