Linux Blog

Searching for multiple strings with grep

Filed under: Shell Script Sundays — at 9:36 pm on Sunday, August 23, 2009

Sometimes when using grep it’s nice to be able to search for more than one string in a file. It’s actually pretty easy to do, if you can remember the simple syntax. Basically, you pipe the terms together in double quotes and escape the pipe.

grep "gnome\|kde" install.log

The above example will search for gnome or KDE. It works for more more than one term, and also works with the invert match -v option, to exclude lines that include gnome or kde.

Todays article extra short, and late, but better late and short than none at all I guess.

Man Pages for commands in this post »



Comment by Gen2ly

August 24, 2009 @ 12:15 am

hey, that’s a great tip, always wondered if there was a way to do this.

Comment by shaktale

August 24, 2009 @ 4:35 am

You can invoke grep -E without escape the pipe.

With -E you can use extended regulars expressions.

Also you can use egrep what is the same that grep -E, although, direct invocation of egrep is deprecated.


Comment by Rajeesh

August 24, 2009 @ 5:04 am

grep -E “gnome|kde” would also do – extended regex.

Comment by Tyboon

August 24, 2009 @ 8:09 am


You can also use the following synthax :
grep -e gnome -e kde install.log

Comment by

August 24, 2009 @ 9:31 am

@TYBOON @RAJEESH @SHAKTALE Thanks! Obviously you guys are experienced in grepfoo. I’m not even sure If I’d earn my first stripe :)

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>