Grep ‘OR’ regex problem

grepregular expression

I am trying to use grep with a regex to find lines in a file that match 1 of 2 possible strings. Here is my grep:

$ grep "^ID.*(ETS|FBS)" my_file.txt

The above grep returns no results. However if I execute either:

$ grep "^ID.*ETS" my_file.txt  

or

$ grep "^ID.*FBS" my_file.txt  

I do match specific lines. Why is my OR regex not matching? Thanks in advance for the help!

Best Answer

With normal regex, the characters (, | and ) need to be escaped. So you should use

$ grep "^ID.*\(ETS\|FBS\)" my_file.txt

You don't need the escapes when you use the extended regex (-E)option. See man grep, section "Basic vs Extended Regular Expressions".

Related Question