Better than grep

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!

7 thoughts on “Better than grep”

  1. Hi, you can add the following in your bashrc file,

    alias grep=’grep –exclude-dir=.svn’

Comments are closed.