[Linux-disciples] grep more than one phrase

Adam Rosi-Kessel adam at rosi-kessel.org
Wed Apr 23 12:41:22 EDT 2008


Jamie Forrest wrote, on 4/23/2008 12:08 PM:
> is there a way to grep for more than one phrase? or maybe i should use  
> a different util for that?
> 
> in other words i want to do something like:
> 
> grep -r foo bar ~/public_html

Do you mean grep for both or either?

grep -r "foo\|bar" ~/public_html

will show all hits with *either* "foo" or "bar"

grep -r "foo.*bar" ~/public_html

will show all hits with "foo" before "bar" in the same line

grep -r "bar.*foo" ~/public_html

will show all hits with "bar" before "foo" in the same line

grep -r "\(bar.*foo\)\|\(foo.*bar\)" ~/public_html

will show all hits with bar and foo on the same line in any order

If you are getting more complex than that, you should probably use perl.


More information about the Linux-disciples mailing list