Feed on

use an OR in grep

to search for either of two arguments in grep, you’ll need the usual or character: vertical bar “|”. however, you’ll also need to add a backwards slash in front:

$ grep “str1\|str2″ my_file.txt

Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

4 Responses to “use an OR in grep”

  1. on 01 Mar 2008 at 7:11 am Jadu Saikia

    Your whole website is a great work, its very nice :-)

    I have added two similar solutions:

    $ awk ‘/str1|str2/’ file.out
    $ sed -e ‘/str1/b’ -e ‘/str2/b’ -e d file.out


  2. on 01 Mar 2008 at 11:12 am Lawrence David

    thanks for the ideas jadu!

  3. on 25 Mar 2008 at 5:27 pm niko

    What about egrep ?

    $ egrep “str1|str2″ my_file.txt

  4. on 14 Dec 2011 at 8:01 am Googie

    Thanks for the tip, it was just what I was looking for!
    (checking out the rest of your site now)

Did I get this wrong? Let me know!

Trackback URI | Comments RSS