我想谈谈一种可以大大简化生活的搜索实用程序。当我到达服务器并且需要寻找东西时,我要做的第一件事是检查ack是否已安装。该实用程序可以很好地替代grep以及find和wc。为什么不grep?Ack提供了更好的现成设置,更多的人类可读选项,perl常规和配置系统。如果你爱(不得不)通过终端进行搜索,那么您绝对应该尝试一下。
基本特征
默认情况下,Ack是递归的,写更少的选项总是好的。
我们可以使用-w标志来告诉实用程序寻找模板实例,该实例被单词边界(空格字符,斜杠等)包围。
ack -w mysql
![](https://habrastorage.org/webt/ap/pe/_i/appe_i1zeu4dmh-hshiivhxjn5c.png)
Ack支持文件类型搜索。例如,在json文件中找到模块的版本。
ack --json '"version":\s+"\d+\.\d+\.\d+"'
![](https://habrastorage.org/webt/wi/2x/fu/wi2xfuawt6edxkl0oe4eg0qtbto.png)
可以在以下位置找到受支持的文件类型的完整列表:
ack --help-types
通常,必须计算一个短语在日志文件中出现的次数,例如,以了解脚本处理了多少数据。
![](https://habrastorage.org/webt/xy/7j/5a/xy7j5a6fgp25raocnohxbr7bk3o.png)
我们计算该过程在test.log文件中发生了多少次,而不区分大小写(-i)。
, . mysql: - (-) *.js (--js), (-h) .
ack --js -w mysql
ack --js -wch mysql
![](https://habrastorage.org/webt/eg/b2/8g/egb28gobj9itdersa8rwgfyly7k.png)
, - (-l)
ack --js -w -cl mysql
![](https://habrastorage.org/webt/bg/4-/3_/bg4-3_ntd8xjni92wf51jpkzmca.png)
c, ack
(-B) (-A) . - , .
ack --js --column -B 2 "query.once\('" ./lib/
![](https://habrastorage.org/webt/o5/f4/5c/o5f45cgs_fmeawirb0xt3s2jtlq.png)
ack --js --column -A 2 "query.once\('" . /lib/
![](https://habrastorage.org/webt/01/_0/iw/01_0iwlhtoe6pnahjcmwe1yfuc0.png)
, (-)
ack --js --column -C 2 "query.once\('" ./lib/
(-v) , . .
Ack grep Perl .
, .
ack 'var\s+add\s+'
![](https://habrastorage.org/webt/3s/5y/ls/3s5ylses8iqp_ulhktjdluloy-q.png)
ack '\*\s+\[v\d+\.\d+\.\d+\]'
![](https://habrastorage.org/webt/ml/so/5x/mlso5xgrz_jq3kbnxnw-vzvb-cc.png)
, . --output (-o)
ack -o '\*\s+\[v\d+\.\d+\.\d+\]'
![](https://habrastorage.org/webt/a6/yg/z2/a6ygz2_gof_4vk5q0eanxjqcvo8.png)
, output $[ ]. ,
ack --output='version is $1' '\*\s+\[v(\d+\.\d+\.\d+)\]'
![](https://habrastorage.org/webt/r2/yh/6f/r2yh6fr1gqr-cnbyqiddyxgw9uu.png)
Ack --range-start --range-end. ,
, .
, sql
![](https://habrastorage.org/webt/it/8n/um/it8num1htv937uprvsl735a8ydq.png)
. SELECT, FROM.
ack --range-start ^SELECT --range-end ^FROM 't\d+\.' ./test.sql
![](https://habrastorage.org/webt/wt/w2/m-/wtw2m-ml2lpp4cwq6dewyadu1am.png)
, , \, -Q.
ack --json 'mysql\.'
ack --json -Q mysql.
![](https://habrastorage.org/webt/bg/op/ff/bgopffzlbuz0wce8tvfjsnm6ppi.png)
ack -f --js
![](https://habrastorage.org/webt/7j/js/g-/7jjsg-pep5tzrqeckfvwbypysma.png)
js P*, (-g).
ack -g --js '\/Pa.+.js$'
![](https://habrastorage.org/webt/xu/c0/9s/xuc09s7pkujclvjunutl7qdy-qy.png)
. (~/.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) .