Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use grep to find all files that contain one pattern but don't have a second?

Tags:

grep

shell

Is there an easy way using grep (or combine with other standard command line tools) to get a list of all files which contain one pattern yet don't have a second pattern?

In my specific case I want a list of all files that contain the pattern:

override.*commitProperties

yet don't contain:

super.commitProperties

I'm on Windows but use cygwin extensively. I know how to either find all files with the first pattern OR all patterns without the second, but I'm not sure how to combine those queries.

I'd prefer answers that are generic, as I have a feeling plenty of other people could find this type of query useful. It's easy enough for me to take a generic solution and plug in my values. I just included my specific instance to make the explanation easier.

like image 616
Herms Avatar asked Sep 14 '09 15:09

Herms


1 Answers

grep -rl "override.*commitProperties" . | xargs grep -L "super.commitProperties"

-l prints the files with a match

-L prints the files without a match

like image 154
Douglas Leeder Avatar answered Nov 12 '22 05:11

Douglas Leeder