Results tagged “#timesaver” from chronicles of leto

Find Files Who Have No Lines Matching a Regex

|
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!
Clicky Web Analytics Screw you, spammers! 42