Twitter Updates

Tuesday 23 September 2008

Search for files NOT containing text

I have been trying to use grep to find files that dont contain certain text.
And well it has not been going so well, to find files that contain text is easy
$grep -il text *.txt
To invert a grep search just add the option -v, that will be the non matching files?
$grep -ilv text *.txt
What actually happens is it is searching line by line, and invariably the .txt file has more than one line that does not contain the 'text'.
What you actually want to do is find all the files that match (because that is easy) then print out a list of files omitting all the ones that match.
So here is a short ruby script to do just that:
I called the script ruby_find.rb
and call it using $ruby_find.rb text

2 comments:

thy said...

I am aware that this is a pretty old post but if s/o find this and ruby is not an option and your bash supports -L option then it might be of help:
grep -il text *.txt

inverted in bash is:
grep -iL text *.txt

This is what I found after I continued searching becuase ruby is not an option for me.

A Person said...

Cheers thy, the blogger updates also seem to have destroyed the code formatting :), so have embedded as a gist instead.