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:
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:
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:
Or you can redirect output into it:
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:
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:
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.
Our latest tutorials delivered straight to your inbox