Ack: A Better Grep

Spread the love

If you have any experience at all using the Linux command line, you’ve no doubt used “grep” to search for strings of text, whether they come from files or from commands. There’s a better way to do so, and it’s called Ack.

Ack is a program written in Perl that aims to be a replacement for the venerable old Grep, which has been around since the early ’70s. (In case you’re wondering, grep stands for “Global Regular Expression Print.”)

Ack’s author, Andy Lester, is so confident that Ack is better than grep, that his project’s website is named betterthangrep.com.

You can get it in a few ways. If you’re a regular Perl user, you can use CPAN to install it, perhaps with Perlbrew as I outlined in an earlier article. Cpanminus is a popular lightweight CPAN client. To install Ack with it, just use this command:

cpanm App::Ack

Of course, if you’re using Ubuntu and want to install it via apt-get, you’re free to do that as well, though Ubuntu tends to be behind the developer’s latest version. Just use this command:

sudo apt-get install ack-grep

You can use Ack pretty much the way you can using grep. For example, to find the name “maketecheasier” in the file mte.txt, just use this command:

ack maktecheasier mte.txt

Or you can redirect output into it:

cat | ack maketecheasier

One of the major features new users will notice is that it gives a nice visual highlight to your search string in the output, which means you can see exactly if you’re actually matching what you’re looking for.

If you’ve searching with a complicated regular expression, you can see what exactly is matching while also detecting false positives.

Another useful feature is that that Ack automatically searches files recursively. This means that not only will Ack search all the files in a directory if you give it one, if that directory contains other directories, it will search those as well, until you get to the bottom of the directory tree. This is especially useful for programmers (and if you are one, you probably already know what recursive means), but it’s also very useful if you’re looking for a pattern in a bunch of plain text files.

And speaking of programming, you can narrow your search to a variety of popular programming languages. Here’s an example using Perl:

ack --perl 'somepattern' /my/directory

You can find even more examples in the “Ack’s file types” section of the documentation.

Since Ack is written in Perl, it’s automatically compatible with Perl’s regular expressions. Regular expressions is a kind of super-precise text-searching, letting you find very specific text patterns.

The best and funniest feature is the “--thpppt” option, which calls up an ASCII art drawing of Bill the Cat:

_   /|
\'o.O'
=(___)=
   U    ack --thppt!

Since Unix and Linux make it relatively easy to write programs, it’s possible to easily create replacements for common system programs. Ack is one of those that shows the creativity of the Perl and Unix/Linux communities.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe


David Delony

David Delony is a writer for Make Tech Easier

Comments are closed