grep's -e option can be used multiple times to specify more than one pattern which will result in a match, e.g.
Code:
$ cat testfile
one two
two three
three four
five
$ grep -e three -e five testfile
two three
three four
five
You can do a test for lines which match more than one pattern by grepping the output of grep:
Code:
$ cat testfile2
a
ab
abc
bcde
$ grep a testfile2 |grep c
abc
Note that grep will read from standard input if no file name is specified. This is functionally the same as the command above:
Code:
cat testfile2 |grep a |grep c
grep sets the error level to 0 if a math was found, 1 if no match was found, and 2 if there was an error. When several commands together, the last one in the line will determine the error level of the pipeline as a whole, so this works as and:
Code:
$ grep a testfile2 |grep c && echo "we found something" || echo "we did not find anything"
abc
we found something
$ grep a testfile2 |grep f && echo "we found something" || echo "we did not find anything"
we did not find anything
I'd like to clarify the && and || syntax. These look at the value of the error level variable "$?". An example should help clarify what is happening:
Code:
grep ab testfile2 && echo "found ab" || echo "ab not found"
Is the same as doing this:
Code:
grep ab testfile2
if [ $? -eq 0 ]; then
echo "found ab"
else
echo "ab not found"
fi
It's not as easy to read if you are not familiar with the syntax, but it's a lot less typing :-)