Introduction
I have occasionally been using a tool called ack for a
few years now. It’s billed as “an alternative to grep for
programmers”.
There are several features I find particularly useful:
It can restrict text searches to files of a particular
type
It uses Perl regular expressions which may be the most powerful
and feature rich types of RE’s available at present
You can limit the search area within a file if desired
It is a very comprehensive and useful tool, though maybe quite
complex to use. Personally I use it in special cases where I need its
power, and otherwise use the usual grep.
In this episode I will give you the flavour of its capabilities and
otherwise leave you to research more if it sounds interesting.
Installing ack
The tool can be found in repositories. I use Debian, and
ack is in the Debian repo and can be installed with:
sudo apt install ack
Installing it this way the version I have (and am describing here) is
3.6.0. There is a new version, 3.7.0 available from the website.
The documentation on the website suggests installing it as a Perl
module using CPAN, which is something I will do soon I
think.
Perl regular expressions
These are very sophisticated.
A project to convert the Perl regular expression capabilities into a
portable library form was undertaken by Philip Hazel of Cambridge
University in 1997, and was called Perl Compatible Regular
Expressions or PCRE.
Philip Hazel was the originator of the exim mail
transfer agent (MTA, or mail server), and wanted to use PCRE within
it.
Since then PCRE (and later PCRE2) is the way regular expressions are
implemented in a lot of other software, which shows how widespread use
of the Perl RE has become.
The ack documentation refers to the Perl manual for
details of this type of regular expression, and to a tutorial, if you
wish to gain a deeper understanding.
It should be noted that GNU grep can use Perl compatible
regular expressions when matching lines in files, but this feature is
marked as experimental.
File types
The ack command has rules for recognising file types. It
does this by looking at the name extensions ('.html' or
'.py' for example), and in some cases by examining their
contents. The complete list of types can be found by running:
ack --help-types
… or, for a more detailed but less readable list:
ack --dump
Some examples are:
cc for C files
haskell for Haskell files
lua for Lua files
python for Python files
shell for Bash, and other shell command files
These names can be used with the options -t TYPE and
--type=TYPE and also by simply preceding them with two
dashes (--TYPE). There are also ways of requesting files
not of a given type: -T TYPE, --type=noTYPE
and --noTYPE.
To check files in the current directory of type shell an
ack command like the following might be used and the
following type of output produced:
$ ack --shell declare
Bash_snippet__using_coproc_with_SQLite/examples/coproc_test.sh
11:declare -a com=('date +%F' 'whoami' 'id' 'echo "$BASH_VERSION"'
Note that ack reports the file path and numbered lines
within it that match.
You can