I want to talk about a search utility that greatly simplifies life. Whenever I access a server and need to search for something, I first check if ack is installed. This utility is a great alternative to grep, and in some ways, to find and wc. Why not grep? Ack has more user-friendly default settings, more readable options, Perl regex, and a configuration system. If you like (or have to) search through the terminal, you definitely should give it a try.
Basic Features
Ack is recursive by default, and it's always good to write fewer options.
We can use the flag -w, to tell the utility to search for instances of our pattern surrounded by word boundaries (whitespace characters, slashes, etc.).
ack -w mysql
Ack supports searching by file type. For example, let's find the version of a module in JSON files.
ack --json '"version":\s+"\d+.\d+.\d+"'
A complete list of supported file types can be viewed with:
ack --help-typesOften, we need to count how many times a phrase appears in a log file, for example, to understand how much data a script has processed.
![]()
Let's count how many times process appears in the test.log file, ignoring case (-i).
We can count occurrences not just in a specific file, but in a group. Let's enhance the previous search for the word mysql: we'll count the occurrences of the words (-c) in *.js files (—js), excluding files where nothing was found (-h) and summing the total.
# выведем на экран все вхождения
ack --js -w mysql
# считаем общую сумму вхождений
ack --js -wch mysql
In addition, we can get a detailed report on the number of occurrences in each file with (-l)
ack --js -w -cl mysql
If you need additional context for your search, you can ask ack
to show lines before (-B) and after (-A) the found expression. To do this, you need to specify the number of lines to show after the option.
# 2 строки до
ack --js --column -B 2 "query.once('" ./lib/
# 2 строки после
ack --js --column -A 2 "query.once('" . /lib/
And if you need both, use (-C)
ack --js --column -C 2 "query.once('" .\/lib\/There is also an option (-v) for inverting the search, i.e., showing lines that do not contain the specified pattern.
Regular Expressions
Ack uses Perl-compatible expressions, unlike grep.
For me, this is a big plus, as I don't have to memorize a separate syntax for regex.
ack 'vars+adds+'
A more complex example
ack '*\s+[vd+.\d+.\d+]'
Often, you may want to leave only results that match the pattern. The --output option will help here (-o)
ack -o '*\s+[vd+.\d+.\d+]'
Moreover, by using parentheses, we can highlight the found part and refer to it in the output through the variable $[group number]. For example,
ack --output='version is $1' '*s+[v(d+.d+.d+)]'
Ack has useful options --range-start and --range-end. They are helpful when
the data is stored not in one line, but in a multiline format.
For example, there is a file with SQL code

Let's extract the column names. The beginning of the block will be a line starting with SELECT, and the end will be a line starting with FROM.
ack --range-start ^SELECT --range-end ^FROM 'td+.' ./test.sql
If the search expression includes special characters such as a dot, parentheses, and others, in order not to escape them with \, you can use the option -Q.
# Поиск с экранированием
ack --json 'mysql.'
# Поиск без экранирования
ack --json -Q mysql.
File handling
Get a list of files with a specific extension
ack -f --js
Find all JS files whose names start with P*, using the option (-g).
ack -g --js '/Pa.+.js$'
Configuration
The utility has its config file. You can have both a global config for the user (~/.ackrc) and a local one for a specific folder (you need to create a file .ackrc in the folder).
Most options that are specified in the configs can also be manually defined when calling. Let's review a few of them.
Ignore a folder when searching
--ignore-dir=distAdd a custom file type —vue.
--type-add=vue:ext:js,vueNow you can use the option —vue to search in files .vue. For example: ack —vue App.
You can also specify a list of extensions for this option. For example, here when using —vue, . js files will also be processed.
Игнорировать файлы, например, минифицированные *.min.js
--ignore-file=match:/\.min.js$/Installation
CentOS
yum update -y && yum install ack -yUbuntu
apt-get update -y && apt-get install ack-grep -yMac OS
brew update && brew install ackInstallation from the website
curl https://beyondgrep.com/ack-v3.3.1 > ~/bin/ack && chmod 0755 ~/bin/ackPlugins for editors:
Conclusion
This is far from all the capabilities. You can view the complete list of features by executing:
ack --help
# or
ack --manThe ack utility makes searching in the terminal more convenient and flexible. And with the help of a pipeline (ack -C 10 hello | ack world) you can create a powerful combine for searching and filtering data in the file system and within the files themselves.
Source: habr.com
