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!

0 TrackBacks

Listed below are links to blogs that reference this entry: Find Files Who Have No Lines Matching a Regex.

TrackBack URL for this entry:


Mine GNU grep 2.5.3 has option -L --files-without-match print only names of FILEs containing no match. TIMTOWTDI :)

Leave a comment

About this Entry

This page contains a single entry by Jonathan Leto published on February 17, 2009 12:39 AM.

Primes, Primality and Pseudoprimes was the previous entry in this blog.

Sedtris - Tetris Written In Sed is the next entry in this blog.

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

Clicky Web Analytics Screw you, spammers! 42