Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
[Computer]$ grep "foo|bar" filename

I understand the above command should return each line in filename where there exits "foo" or "bar". The man pages confirms | as the Regex or symbol and the code works for "foo" and "bar" independently. What am I missing?

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

grep uses basic regular expressions (BRE) by default. From the man page:

Basic vs Extended Regular Expressions: In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, +, {, \|, (, and ).

So you either have to escape the |:

grep "foo\|bar" filename 

or turn on extended regular expressions:

grep -E "foo|bar" filename
share|improve this answer
    
I saw that in the man page and misunderstood. I thought it meant that \| would make it look for the complete expression "foo|bar" as opposed to the individual expressions. Thanks for the fast response! –  Ocasta Eshu Jun 13 '12 at 1:23
2  
You can also use egrep (an alias for grep -E) instead of grep. –  speakr Aug 2 '12 at 12:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.