Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

share|improve this answer
    
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
share|improve this answer
  • 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... :)

share|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
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .