February 2009 Archives

Find Files Who Have No Lines Matching a Regex

| | Comments (1) | TrackBacks (0)
I found myself in the situation of having a bunch of files and I needed to find out which one's did not include a certain header file. This turned out to be a great place for short-circuiting with grep.

for i in files/*; do grep PATTERN $i >>/dev/null || echo $i; done

Grep returns false when it cannot find a match, which triggers the echo of the filename to STDOUT. The PATTERN can be any valid regular expression for your system's flavor of grep. This can save tons of time if you have dozens/hundreds of files and only a few are missing something. To edit these files in vim, it is nothing more than:

vim `for i in files/*; do grep PATTERN $i >>/dev/null || echo $i; done`

Happy Hacking!

About this Archive

This page is an archive of entries from February 2009 listed from newest to oldest.

January 2009 is the previous archive.

June 2009 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Clicky Web Analytics Screw you, spammers! 42