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?

On many systems, grep, egrep, and fgrep are the same binary (they might or
might not be symlinks or hard links to each other, but the files are often
identical).

However, when this binary is invoked as egrep, you get extended matching.

Otherwise, I think you need the -E flag.

So:

    egrep '^(fee|fie)' junk.txt

    grep -E '^(fee|fie)' junk.txt

Both should work identically.

> Obviously this won't work b/c there are some characters there that are
> meaningful to the shell. According to man grep, I should enclose my
> pattern in single quotes to get around this:
>
> grep '^(fee|fie)' junk.txt
>
> However, this produces no lines of output (even though I know both
> patterns are there).

Because with regular grep, you're not using the extended metacharacter
set, so the command is looking for the literal pattern (or something close
to it anyway).

But you're right, you do need to wrap any moderately fancy patterns in
single or double quotes to protect that pattern from the shell.


Clearer?



-- 
Chris Devers      cdevers at pobox.com
http://devers.homeip.net:8080/blog/

pun moratorium, n.
The doomed campaign to deoxymoronize computer humor.

In particular, the vain attempt to demonstrate that plays on words such
as RISC and UNIX are unfunny if the player is unaware of their
historically built-in playfulness. Other puns assinorum deserving a
well-earned retirement relate PARADIGM, Paradise, and ten sents in
boringly obvious ways: "Paradigms Lost and Regained," "Brother, can you
s'paradigm?" and so on. Likewise, the cash/CACHE thing is surely
bankrupt: "Cache-only memory, no checks." Be assured, too, that every
known C homophone has had its weary, C-sick day at the C-Users
Journal's annual C-pun contest: C-through UNIX, C'est C Bon, Holy C,
Proficient C, Vitamin C, O say can you C? e = mC^2, Variations in C,
C-C Rider, Rauchen C?, The Cruel C from Cmantec, Mer-C Beaucoup... ad
nau-C-am.

One of Western Democracy's major flaws is that we cannot, without
pettifogging legal interference, publically hang, draw, and quarter
C-punsters.

    -- from _The Computer Contradictionary_, Stan Kelly-Bootle, 1995




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