Thank You, grep!

by chromatic

I spend a lot of time searching text files. More accurately, I spend a lot of time searching nested directories of text files. For source code, I know I should use Ctags, but I've never quite made the switch.

For plain text files (books, articles, stories, weblog entries, notes, contracts, et cetera), I'm still a GNU grep fan.

I spent a few hours in the past week editing a book manuscript and producing well-formed and valid DocBook XML. (I wrote two books in DocBook XML. While it's a great file format for producing a book, it's a face-stabbingly hateful format for actually writing a book.) Unfortunately, the conversion process to DocBook revealed some problems in the source material. In specific, certain links from one part of the manuscript to others were invalid.

I needed to find and fix the dangling links in all fifteen book chapters, spread out in several dozen individual files. Grep and a little bit of command-line magic made the task much, much easier. I ended up with the pattern:

vi $( grep -l 'L<refactoring_strategies>' ?_*/*.pod)

That is, search all of the .pod files in directories whose names start with one character and an underscore. For all of those files which contain a link to an anchor named refactoring_strategies, print their names. Open that list of files in Vim.

I still had to edit plenty of text, but finding only the files I needed saved me a tremendous amount of time. Throw in grep's -r (recurse into subdirectories) and -i (use a case-insensitive match) switches, and I'm very happily productive.

Thank you to everyone who's contributed to grep and GNU grep through the years. Your work helps me work, every day.


Adriano Ferreira
2007-06-05 06:28:48
ack as a pure Perl implementation of this kind of functionality brought this happiness to OS-challenged people as well. You know, those scared of installing one more command-line tool in those systems unfriendly to CLI programs. Unfortunately, installing Perl and configuring the CPAN client (from a non-aficionado point of view) is not as common and easy as it could be.
2007-06-05 11:48:10
Thus we have the power of Unix/Linux. Combined with pipelines you can do very powerful things with (seemingly) simple commands.

Keith Fahlgren
2007-06-05 15:00:33
(I wrote two books in DocBook XML. While it's a great file format for producing a book, it's a face-stabbingly hateful format for actually writing a book.)

Maybe, but slightly less so with good vim macros.

2007-06-05 15:24:25
I find 'sed' indispensable as well.
( )
2007-06-05 15:47:04
I'm a big fan of etags / ctags, but they only cover definitions. Grep is still massively useful. I commonly use grep with Emacs macros for bulk repetitive edits. I just did that today to replace several references to a certain file whose name had changed. I don't imagine grep will ever leave my toolbox.

Note also that Windows systems ship with something called `findstr' which supports /S and /I options. It's nowhere near as powerful as grep, but it's nice to have when you're on a Windows system.

2007-06-05 17:47:13
Note that, VIM's (internal/external) grep support is also great. You could use the same grep command from within VIM (no need for the '-l' switch and also no need to start the command with '!'):

:grep 'L' ?_*/*.pod

What you get from using this method is the quickfix mode. Just invoke the above command and try ':cnext/:cprev/:clist'. See ':h :grep' and ':h :vimgrep' for more...

Badai Aqrandista
2007-06-05 18:19:47
my favourite to search and replace is grep, xargs, and perl combination:

grep -rl 'search this' . | xargs perl -pi -e's/this/that/g'

2007-06-06 11:08:20
You might want to check out supgrep (if you have qt3.x installed on your system). It searches for text while you type. It has 4 text entry boxes. Type text in the left most text entry and it will search the files listed in the right most text entry.

2008-06-09 09:03:39
great article, great informations like (mostly) everytime chromatic, but tell me something: whats about the "thank you,..."-thing? is it a runnĂ­ng gag? :) many of your posts starts so (i enjoy reading them)...

keep on the great work! "thank you, chromatic" ;)
Paul "Push it"-WebDevelompment