[TriLUG] Learn something new every day.

Aaron S. Joyner aaron at joyner.ws
Wed Feb 22 10:23:49 EST 2006


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)"

although I have to admit, if I wanted to match on both things in
_either_ order I'd probably just go with the original poster's
suggestion, which should always work.  I don't see what situation caused
it to fail for the original poster:

perl -e 'print "bar"; print "x" x 100000000; print "foo"; ' | grep foo |
grep bar | wc

even with 100 million characters* in between them, you still match the
line.  This is true because even though the line may not come through
the buffer all at once, that's okay, because only "matched" lines will
come out of the first grep into the pipe, and the second grep will
buffer the line internally all the way up to a new line before it
considers the entire line "not a match" and throws it away.  I can't
explain the particular behavior you saw with out more information, of
course.

Aaron S. Joyner

* - I tried to pull it off with a billion characters, but the machine I
was running it on ran out of ram.  I'll try it when I get to work and
have more ram to play with.  :)



More information about the TriLUG mailing list