Thursday, November 12, 2009

Simple way to do alternation with grep (command line)

I know that the | character is supposed to be used for alternation, but I just can't seem to get it to work with command line grep. The easy way I've found to do alternation is to use -e switch like so:

grep -e someregex1 -e someregex2 file.txt

Using more than one -e on the command line implies alternation.


Anonymous said...

Alternations through the vertical bar are not supported in grep's default mode. You need to either use the "-E" option (which enables extended mode) or invoke "egrep" which is exactly equivalent.

I feel like a beefy miracle said...

or escape it \|
e.g. grep reg_expr1\|reg_expr2