grep question: either of 2 patterns?

John Chambers jc at trillian.mit.edu
Tue Sep 2 13:01:35 EDT 2003


| On Tue, 2 Sep 2003, Ken Gosier wrote:
|
| > I want to use grep to match on lines in a file that begin with either of 2
| > patterns. As far as I understand regex's, I should use ^(...|...) to do
| > this, as in:
| >
| > grep ^(fee|fie) junk.txt
|
| Tried egrep yet?
...

That was a fine  answer.   It's  useful  to  get  familiar  with  the
differences between the various grep clones.  This also will probably
work:

grep '^\(fee\|fie\)' junk.txt

The explanation is that, by default, parens match themself.   If  you
want  to use them as grouping operators with grep, you need to escape
them.  This is exactly the reverse of perl's handling  of  parens  in
REs.   This  was  reversed intentionally in perl, because people felt
that parens are mostly used for grouping in REs, and it would save  a
lot  of  typing  over  the  years  if  you  don't  need  to  type the
backslashes in patterns like the above.  Instead, you have to  escape
parens to match them, but how often do you want to do that?

Also, something that I did years ago  was  to  write  a  simple  perl
script  that  mimics the basic grep functionality, but with perl REs.
You can call perl with command-line options to do this, but I find it
easier to just put the script under the name "pgrep". I know perl REs
well enough that this is very useful to me.  My script is at:

http://trillian.mit.edu/~jc/sh/pgrep

(You can see how old it is from the suggestion that people upgrade to
perl 5. ;-)





More information about the Discuss mailing list