Better than grep

July 11, 2008 – 13:32

If you’re a programmer and you’re using a command line heavily, you’ve certainly come across a big nuisance in the usage of GNU’s grep utility: The verbosity you need to exclude files from being searched, such as backup files (*~ or *#) or any kind of vcs inventory stupidity like .svn or .cvs. (Did I mention earlier that this is one of the many reasons why I hate svn and cvs with a passion - for the fact that they clutter my workspace with this crap?)

Anyways, with grep you usually end up with something like that:

$ grep -R myterm | grep -v ‘\.svn’ | grep -v ‘~:’

or, to speed up your searches a bit and let grep not even crawl things you don’t like to see anyways:

$ grep -R myterm `find . -type f | grep -v ‘\.svn’ | grep -v ‘~:’`

Anybody else thinks that this syntax is just hilarious and totally overkill? So I looked at grep’s manpage for some kind of .greprc which I could define in my homedir and in which I could set all the things I want to exclude, but apparently no such file is acknowledged by grep.

Finally I did a Google search for “grep ignore svn directories” and found ack - a Perl tool which resolves this and other problems with grep. My personal feature highlights:

  • You can use real Perl regular expressions (no grep -r stuff needed)
  • -A, -B and -C options work just like I know them from grep
  • Output is highlighted with terminal colors and very much cleaned up in comparison with grep
  • There are predefined sets of file extensions to search, i.e. ack --php foo will search all php files (php3, php4, php, aso.) for foo, also with an option to exclude these sets with f.e --noperl

So I’m more than satisfied with this - if I would have only found this earlier! This saves my day!

Oh, I think I forgot to mention one absolute killer feature of ack - to quote the author:

Command name is 25% fewer characters to type! Save days of free-time! Heck, it’s 50% shorter compared to grep -r.

Go, get it while its hot!

  1. 4 Responses to “Better than grep”

  2. Will you be making a MacPorts port for ack? :)

    By Ryan Schmidt on Jul 12, 2008

  3. Heh, somebody is reading my blog ;)

    I’ll try my best and make a new port for this.

    By tommyd on Jul 12, 2008

  4. Actually, somebody was faster than me:

    http://trac.macports.org/browser/trunk/dports/perl/p5-app-ack/Portfile

    By tommyd on Jul 16, 2008

  5. Thanks for the note about the Macport. I added it to the Ack home page.

    By Andy Lester on Jul 16, 2008

Post a Comment