Wednesday, November 18, 2009

Neato source grepper!

I don't think this is anything too novel, but I just came up with this function for my ~/.bashrc to make grepping through source trees easier. Typically I egrep -nr $REGEX $PATH, but then the results are polluted with vim backup files and sometimes object files. With this little function in place I can simply agrep $REGEX $PATH and get none of that junk:

local argstr=
local first=1
local i=
for i in "$@"
if [ $first = 0 ]; then

echo -ne "$argstr" | xargs -0 egrep -nr | egrep -v '~:[0-9]+:|^Binary file'

I suppose the caveat is that if you program in a language where constructs matching ~:[0-9]+: are legal this could filter out some source. Entering stream-of-consciousness... Of course, even in C/C++ they're legal within strings. Hm. Filtering the output like that to filter files is brain-dead, using find to filter files up front would be better... but then you'd hit the command-line length limit... I bet someone has written a tool for this... probably using Perl... Leaving stream-of-consciousness

Indeed, there is ack. And, of course, it's in Perl. It is way better than my little bash hack.

EDIT: It turns out that I totally missed missed two options to GNU grep that would have saved me the trouble of writing anything but an alias: -I (short for --binary-files=without-match) and --exclude='*~' to get rid of those vim files. At least I got to learn how xargs works.

No comments: