views:

1016

answers:

8

I'm using the following command in my web application to find all files in the current directory that contain the string foo (leaving out svn directories).

find .  -not  -ipath '.*svn*' -exec  grep -H -E -o  "foo"  {} \; > grep_results.txt

How do I find out the files that doesn't contain the word foo?

A: 

$man grep

-v, --invert-match

Invert the sense of matching, to select non-matching lines.

Ubersoldat
+1  A: 

Use grep's -v flag :)

lorenzog
+2  A: 

Ok....the inverse matching on grep is -v, have you tried that?

tommieb75
+1  A: 

If your grep has the -L option:

$ grep -L "foo" *
ghostdog74
+2  A: 

Finally I got it right using this as suggested by a friend:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep 0

It gives me all the files that has zero match for foo :)

tony_le_montana
You want to change the grep 0 at the end to grep 0$ (otherwise you get erroneous matches on files that have the character 0 in their filename).
clouseau
A: 

I had good luck with

grep -H -E -o -c "foo" */*/*.ext | grep ext:0

My attempts with grep -v just gave me all the lines without "foo"

Johnny
A: 

Your friend was "sort of" right. You will actually need:

find .  -not  -ipath '.*svn*' -exec  grep  -H -E -o -c  "foo"  {} \; | grep :0\$

Otherwise, you will get hits on files that have 10 lines w/ "foo", files that have 100 lines w/ "foo", etc. as well as lines that have a "0" in the filename like "my_10_best_friends.txt".

Forrest Tiffany
A: 

Please take a look at ack at http://betterthangrep.com/. It does the .svn exclusion for you automatically, gives you Perl regexes, and is a simple download of a single Perl program. The equivalent of what you're looking for should be, in ack:

ack -v foo
Andy Lester