Ack is better than grep

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 is better than grep

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+"'

Ack is better than grep

A complete list of supported file types can be viewed with:

ack --help-types

Often, 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.

Ack is better than grep
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

Ack is better than grep

In addition, we can get a detailed report on the number of occurrences in each file with (-l)

ack --js -w -cl mysql

Ack is better than grep

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/

Ack is better than grep

# 2 строки после 
ack --js --column -A 2 "query.once('" . /lib/

Ack is better than grep

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+'

Ack is better than grep

A more complex example

ack '*\s+[vd+.\d+.\d+]'

Ack is better than grep

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+]'

Ack is better than grep

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 is better than grep

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

Ack is better than grep

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

Ack is better than grep

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.

Ack is better than grep

File handling

Get a list of files with a specific extension

ack -f --js

Ack is better than grep

Find all JS files whose names start with P*, using the option (-g).

ack -g --js '/Pa.+.js$'

Ack is better than grep

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=dist

Add a custom file type —vue.

--type-add=vue:ext:js,vue

Now 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 -y

Ubuntu

apt-get update -y && apt-get install ack-grep -y

Mac OS

brew update && brew install ack

Installation from the website

curl https://beyondgrep.com/ack-v3.3.1 > ~/bin/ack && chmod 0755 ~/bin/ack

Plugins for editors:

Conclusion

This is far from all the capabilities. You can view the complete list of features by executing:

ack --help
# or
ack --man

The 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

Buy reliable website hosting with DDoS protection, VPS VDS servers 🔥 Buy reliable website hosting with DDoS protection, VPS VDS servers | ProHoster