0

From grep's manual

‘-e pattern’
‘--regexp=pattern’

This can be used to specify multiple search patterns, or to protect a pattern beginning with a ‘-’. (‘-e’ is specifi ed by POSIX.)

What does "specify multiple search patterns" mean?

Does it mean exactly to output each line which contains all the search patterns specified by -e? If yes, does it do the same thing as piping multiple greps, each of which matches one of the patterns?

Thanks.

  • Works just as or operator. Especially usefull in case of patterns starting with dash (i.e man awk |grep C10 -e "-F" -e "OFS" – George Vasiliou Feb 16 '17 at 23:03
3

To clarify the existing comments and answer: no, it doesn't work the way the OP described.

In fact:

grep -e foo -e bar -e baz myfile

prints out the lines containing either foo or bar or baz.

grep foo myfile | grep bar | grep baz

prints out the lines containing foo and bar and baz (not necessarily in this order).

  • 3
    Your second command line should read grep foo myfile | grep bar | grep baz, otherwise, its output would be the one of grep baz myfile as grep ignores its stdin when one or more file names are provided as arguments. – jlliagre Aug 14 '17 at 14:33
  • @jiliagre Oops. Good catch. Fixed, and thank you. – dr01 Aug 14 '17 at 19:40
2

No, it doesn't mean the patterns are ANDed together.

What it means is that you can specify multiple patterns, without the second and subsequent patterns being interpreted as filenames.

Consider the following:

grep pattern1 file1

Simple enough. Now consider:

grep pattern1 file1 file2

And:

# This doesn't work
grep pattern1 pattern2 file1 file2
# But this does.  There's no difference that's obvious to a computer.
grep pattern1 file1 file2 file3

Do you see the problem?

But, with -e, you can do:

grep -e pattern1 -e pattern2 file1 file2
1

From the POSIX grep description:

By default, an input line shall be selected if any pattern [...] matches any part of the line excluding the terminating <newline>.

  • 6
    Translation: multiple patterns are OR-ed together, while piping multiple greps means AND-ing together the corresponding patterns. – Satō Katsura Feb 16 '17 at 5:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.