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.


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

  • This did not work for me on OS X when I did pmset -g sched | grep -E "shutdown|restart|sleep". (Neither did using egrep.) – 2rs2ts Jun 18 '13 at 19:29

You need to escape the pipe character:

grep 'res\|rep' file

or use multiple -e options:

grep -e 'res' -e 'rep' file
  • The regex sort of way is:

    EDIT 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... :)

  • 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
  • 2
    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 '12 at 20:44
  • Added double quotes to ensure pattern being interpreted well. – Dlf Sep 24 '12 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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