Home > unix > ack – Better than grep?

ack – Better than grep?

I stumbled onto a really nice command line tool named ack while reading a StackOverflow question yesterday.  Living at the domain betterthangrep.com/, it purports to .. be better than grep.  Or, as they put it

ack is a tool like grep, designed for programmers with large trees of heterogeneous source code

I’ve written previously about how to combine find and grep, and really, ack exists to obviate the use of find and grep.  It ignores commonly ignored directories by default (e.g. all those .svn metadata folders that SVN insists on creating), and with a simple command line flag you can tell ack what sort of files you want searched.  Furthermore, because it recurses by default, you don’t need to use the find command to traverse the tree.

Using the todo example, a basic way of searching for the TODOs in all of our java files is to use the command

find . -name "*.java" -exec grep -i -n TODO {} \;

In ack, this is accomplished much easier:

ack -i --java TODO

Furthermore, the matching results are highlighted right away, making it extremely apparent where the matches occur.

I’m going to start using this at work and see if it can replace my grep/find hackery.  Will let you know.  Very impressed so far.


If you want to give it a try, the easiest way to install it is with macports:

port install p5-app-ack
Categories: unix Tags: , , , , , , ,
  1. March 22, 2011 at 3:48 pm

    I love ack! It’s saved me so much time…

    A quick note about installing on a Mac. I don’t use Macports, so this was by far the easiest way for me to install (in Terminal):

    mkdir ~/bin
    curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 !#:3

    All done! Requires no libraries (except curl), takes about 8 seconds🙂

  2. October 4, 2011 at 6:35 am

    Ack is very popular, although I don’t like it, because with a single line of config, grep does everything it does, and is actually faster, despite ack’s loud claims to the contrary:

    $ cat `which grp`
    #!/usr/bin/env bash
    grep -rI –color –exclude-dir=\.bzr –exclude-dir=\.git –exclude-dir=\.hg –exclude-dir=\.svn –exclude-dir=build –exclude-dir=dist –exclude=tags $*

    I did this once in the 1980s, and since then grep works just fine for me. (I have some python-specific excludes in there. No doubt you’ll choose some idiosyncrasies of your own as you work.)

    You could use an alias instead of a script, but I like to create an executable script like this, then I can invoke it from other programs like vim, so that my search within vim works just like my searches at the command-line. You shouldn’t use the GREP_OPTIONS environment variable, because this affects all invocations of grep, even those embedded within other tools, which may break those tools.

    Since grep uses blacklists (as specified above) instead of whitelists, it doesn’t unexpectedly omit correct results like ack does in cronfiles or Makefiles.

    If you set up a handful of common blacklists to be used as default (e.g. skip ‘.git’ directories and -I to skip binary files) then grep is actually faster, despite ack’s loud claims to the contrary.

    Grep will highlight matches in –color.

    Grep will recurse into subdirs (-r)

    In short, almost every single one of the ‘reasons to prefer ack’ listed on its homepage are actually undesirable, misleading or downright untrue. Grep does everything ack does, better and faster, plus with decades of bugfixes and optimizations and compatibility and a community of knowledge. The whole Ack project is a massive waste of time that should never have been created.

  3. November 13, 2014 at 9:05 am

    The soil youu putt imto your raised garden beds is very important, especially
    if youu want an organic garden soil. We show you howw
    to create a long trench in the soil in which you can llay
    the plant on its side, which is a perfect wway to give the plant
    what it needs to grow and flourish. All you need to do is poke little holes into the bottom of plastic 1 gallon jugs,
    bury the jugs next to your plants and make sure to leave an opening.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: