[TriLUG] Learn something new every day.

Rick DeNatale rick.denatale at gmail.com
Wed Feb 22 12:11:23 EST 2006


On 2/22/06, Aaron S. Joyner <aaron at joyner.ws> wrote:
> Rick DeNatale wrote:
>
> >Multiple expressions -e foo -e bar will display lines which contain
> >either foo or bar, as will the equivalent 'grep -F foo bar'
> >
> >One way to do this is
> >
> >grep -e 'foo.*bar' -e 'bar.*foo'
> >
> >You should also be able to do
> >
> >grep -e 'foo.*bar|bar.*foo'
> >
> >but I can't seem to get that to work.
> >
> >
> Where most people go wrong here is they miss the '-e'.  Note that the
> following only works as an "extended" regular expression, via either
> `egrep` or `grep -e`.  Try it this way:
>
> echo -e "foo baz bar\n foo baz" | egrep "(foo.*bar|bar.*foo)"
>

Hmmmm

rick at bill:~$ echo -e "foo baz bar\n foo baz" | egrep "(foo.*bar|bar.*foo)"
foo baz bar
rick at bill:~$ echo -e "foo baz bar\n foo baz" | egrep "foo.*bar|bar.*foo"
foo baz bar
rick at bill:~$ echo -e "foo baz bar\n foo baz" | grep -e"(foo.*bar|bar.*foo)"
rick at bill:~$ echo -e "foo baz bar\n foo baz" | grep -e "(foo.*bar|bar.*foo)"
rick at bill:~$ echo -e "foo baz bar\n foo baz" | grep -e "foo.*bar|bar.*foo"

BUT

rick at bill:~$ echo -e "foo baz bar\n foo baz" | grep -E "foo.*bar|bar.*foo"
foo baz bar


So it really needs to be -E not -e

On the other hand man grep says:

=======

Grep  searches  the  named  input FILEs (or standard input if no files
are named, or the file name - is given) for lines containing a match
to the given PATTERN.  By default, grep prints the matching  lines.

 In  addition,  three  variant  programs  egrep,  fgrep  and  rgrep
are available.  Egrep is the same as grep -E.  Fgrep is the same as
grep -F.  Rgrep is the same as grep -r.

...

       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (see below).

       -e PATTERN, --regexp=PATTERN
              Use PATTERN as the pattern; useful to protect patterns beginning
               with -.
       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (see
below).  This is
              the default.
...
Grep understands three different versions  of  regular  expression 
syntax:  "basic,"  "extended,"  and "perl."   In  GNU grep, there is
no difference in available functionality using either of the first two
syntaxes.  In other implementations, basic  regular  expressions  are 
less  powerful.   The following description applies to extended
regular expressions; differences for basic regular expressions are
summarized afterwards.  Perl regular expressions add additional
functionality, but the implementation used here is undocumented and is
not compatible with other grep implementations.

====

So now I try
rick at bill:~$ echo -G "foo baz bar\n foo baz" | grep -G "foo.*bar|bar.*foo"
rick at bill:~$

So despite the statement that GNU grep makes no distinction between
basic and extended RE syntaxes, this doesn't seem to be the case.

--
Rick DeNatale

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/



More information about the TriLUG mailing list