How can I use grep to search for line with either 'res' or 'rep'?

i try "grep -e res|rep" or gre -e "rep|rep" but that does not work.

link|improve this question

62% accept rate
feedback

3 Answers

You can also use -E option with grep, so there's no need to escape "|", for example:

grep -E 'res|rep' file

Or, you can use egrep, which is the same thing as grep -E:

egrep 'res|rep' file

link|improve this answer
feedback

You need to escape the pipe character:

grep 'res\|rep' file

or use multiple -e options:

grep -e 'res' -e 'rep' file
link|improve this answer
feedback
  • The regex sort of way is:

    grep -nis re[sp] <FILENAME>
    
  • This way you will be presented with all lines containing either "res" or "rep"

  • also note that -nis is in no way important here, I just like it that way... :)

link|improve this answer
1  
Probably you want to escape re[sp] so that it is not accidentally expanded by the shell should there be files named res or rep. – hlovdal May 20 '10 at 18:32
This is not necessary since it is first after the options list, and as such s interpreted as a pattern. – Dlf May 21 '10 at 10:38
hlovdal's comment is correct. The given answer will break if files called 'res' and 'rep' exist, because the shell will convert the command-line into "grep -nis rep res <FILENAME>". Hence you will not find any occurences of 'res' - instead you will search for 'rep' in files 'res' and <FILENAME>. – Jonathan Hartley Mar 18 at 20:44
feedback

Your Answer

 
or
required, but never shown

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