Boston Linux & Unix (BLU) Home | Calendar | Mail Lists | List Archives | Desktop SIG | Hardware Hacking SIG
Wiki | Flickr | PicasaWeb | Video | Maps & Directions | Installfests | Keysignings
Linux Cafe | Meeting Notes | Blog | Linux Links | Bling | About BLU

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

grep question: either of 2 patterns?



| 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. ;-)






BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org