Ack better grep

I want to talk about one search utility that greatly simplifies life. When I get to the server and I need to look for something, the first thing I do is check if ack is installed. This utility is a great replacement for grep, as well as find and wc to some extent. Why not grep? Ack has nicer settings out of the box, more human-readable options, perl regulars, and a config system. If you love(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 -w flag to tell the utility to look for an instance of our template, surrounded by word boundaries (white space, slash, etc.).


ack -w mysql


Ack supports file type searches. For example, find the version of the module in json files.


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


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


ack --help-types

Often it is necessary to calculate how many times a phrase occurs in a log file, for example, to understand how much data the script processed.



We count how many times the process occurs in the test.log file, not case sensitive ( -i ).


, . mysql: - (-) *.js (--js), (-h) .


#     
ack --js -w mysql
#    
ack --js -wch mysql


, - (-l)


ack --js -w -cl mysql


c, ack
(-B) (-A) . - , .


# 2   
ack --js --column -B 2 "query.once\('" ./lib/


# 2   
ack --js --column -A 2 "query.once\('" . /lib/


, (-)


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

(-v) , . .



Ack grep Perl .
, .


ack 'var\s+add\s+'



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


, . --output (-o)


ack -o '\*\s+\[v\d+\.\d+\.\d+\]'


, output $[ ]. ,


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


Ack --range-start --range-end. ,
, .


, sql



. SELECT, FROM.


ack --range-start ^SELECT --range-end ^FROM 't\d+\.' ./test.sql


, , \, -Q.


#    
ack --json 'mysql\.'    
#   
ack --json -Q mysql.




ack -f --js


js P*, (-g).


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



. (~/.ackrc), ( .ackrc).


, . .



--ignore-dir=dist

--vue.


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

--vue, .vue. : ack --vue App.
. , --vue
.js .


, , *.min.js


--ignore-file=match:/\.min\.js$/


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


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

:




. , :


ack –-help
# 
ack --man

ack . pipeline (ack -C 10 hello | ack world) .


All Articles