View Full Version : Quick grep question: matching "word1 and not word2"

December 8th, 2008, 05:23 PM
Anyone know how to match a line of text that has one word but not another:

e.g. "match all lines that have the word red but do not have the word circle."

I can do it with two calls to grep:

grep word1 | grep -v word2

But I imagine there is a better way to match this sort of expression.

Any ideas? Any advice would be greatly appreciated.


December 8th, 2008, 10:26 PM
If by "a better way" you mean just one call instead of two, then it's probably not possible with grep. Grep allows you to use only one regular expression, and you can't express a condition such as "(A) AND NOT (B)" with a single regexp.

So, you need something that can handle two regular expressions -- for example awk:

awk '/word1/ && !/word2/' file