Ack better grep

I want to tell you about a search utility that greatly simplifies life. When I get to the server and I need to look for something, the first thing I check is if ack is installed. This utility is an excellent replacement for grep, as well as to some extent find and wc. Why not grep? Ack has nicer settings out of the box, more human readable options, perl regular expressions and a config system. If you like (have to) search through the terminal, then you definitely should try it.

Basic features

Ack is recursive by default, and writing fewer options is always good.

We can use the flag -wto tell the utility to look for an instance of our template surrounded by word boundaries (whitespace, slashes, etc.).

ack -w mysql

Ack better grep

Ack supports searching by file type. For example, let's find the module version in json files.

ack --json '"version":s+"d+.d+.d+"'

Ack better grep

For a complete list of supported file types, see:

ack --help-types

Often you need to count how many times a phrase occurs in a log file, for example, to understand how much data the script has processed.

Ack better grep
We count how many times process occurs in the test.log file, case insensitive (-i).

We can count occurrences not just in one specific file, but in a group. Let's refine the previous search for the word mysql: count the number of occurrences of words (-from) in *.js files(--js), excluding files in which nothing was found (-h) and summing up the total.

# Π²Ρ‹Π²Π΅Π΄Π΅ΠΌ Π½Π° экран всС вхоТдСния
ack --js -w mysql
# считаСм ΠΎΠ±Ρ‰ΡƒΡŽ сумму Π²Ρ…ΠΎΠΆΠ΄Π΅Π½ΠΈΠΉ
ack --js -wch mysql

Ack better grep

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

ack --js -w -cl mysql

Ack better grep

If you need additional search context, you can ask for ack
show lines before (-B) and after (-A) of the found expression. To do this, specify the number of lines to be displayed after the option.

# 2 строки Π΄ΠΎ 
ack --js --column -B 2 "query.once('" ./lib/

Ack better grep

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

Ack better grep

And if you need both, then use (-FROM)

ack --js --column -C 2 "query.once('" ./lib/

There is also an option (-v) to invert the search, i.e. show lines in which there is no given pattern.

Regular expression

Ack, unlike grep, uses Perl compatible expressions.
For me, this is a big plus, I don’t have to remember a separate syntax for regular expressions.

ack 'vars+adds+'

Ack better grep

More complex example

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

Ack better grep

Often you want to leave in the results only what matches the template. This is where the --output (-o)

ack -o '*s+[vd+.d+.d+]'

Ack better grep

In addition, with the help of parentheses, we can select the found part and refer to it in the output through the $[group number] variable. For example,

ack --output='version is $1' '*s+[v(d+.d+.d+)]'

Ack better grep

Ack has useful options --range-start ΠΈ --range-end. They help when
data is stored not in one line, but in multiline form.

For example, there is a file with sql code

Ack better grep

Extract the column names. The beginning of the block will be a line starting with SELECT, and the end of the line starting with FROM.

ack --range-start ^SELECT --range-end ^FROM 'td+.' ./test.sql

Ack better grep

If the search expression contains special characters such as period, parenthesis and others, then in order not to escape them with , you can use the option -Q.

# Поиск с экранированиСм 
ack --json 'mysql.'    
# Поиск Π±Π΅Π· экранирования
ack --json -Q mysql.

Ack better grep

Work with files

Get a list of files with a specific extension

ack -f --js

Ack better grep

Find all js files whose name starts with P* using the option (-g).

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

Ack better grep

Configuration

The utility has its own config file. You can have both a global config for the user (~/.ackrc) and a local config for a specific folder (you need to create a .ackrc file in the folder).

Most of the options that are registered in the configs can be manually registered when called. Let's analyze a few of them.

Ignore folder when searching

--ignore-dir=dist

Let's add a custom file type --vue.

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

You can now use the --vue option to search files .vue. For example: ack --vue App.
In doing so, you can specify a list of extensions for this option. For example, here when using --vue will be processed and
.js files.

Ignore files like minified *.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 site

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

Plugins for editors:

Conclusion

These are not all possibilities. The full list of features can be viewed by running:

ack –-help
# ΠΈΠ»ΠΈ
ack --man

The ack utility allows you to make searching in the terminal more convenient and flexible. And with the help of pipeline (ack -C 10 hello | ackworld) you can create a powerful combine to search and filter data in the file system and in the files themselves.

Source: habr.com

Add a comment