UNPKG

2.63 kBMarkdownView Raw
1ack
2===
3
4比grep好用的文本搜索工具
5
6## 安装
7
8```shell
9# ubuntu下要安装ack-grep,因为在debian系中,ack这个名字被其他的软件占用了。
10sudo apt-get install ack-grep
11# alpine Linux-apk软件包管理器 安装 ack
12apk install ack
13```
14
15## 参数
16
17这些参数在linux上的适用频率是相当高的,尤其是你用vim做为IDE的话
18
19```shell
20-c(统计)/ -i(忽略大小)/ -h(不显示名称)/
21-l(只显文件名)/ -n(加行号)/ -v(显示不匹配)
22```
23
24## 特点
25
26ack官网列出了这工具的5大卖点:
27
281. 速度非常快,因为它只搜索有意义的东西。
292. 更友好的搜索,忽略那些不是你源码的东西。
303. 为源代码搜索而设计,用更少的击键完成任务。
314. 非常轻便,移植性好。
325. 免费且开源
33
34## 实例
35
36在记忆的时候大体上可以分为这几个部分:
37
38> Searching 代码搜索
39> Search output 搜索结果处理
40> File presentation 文件展示
41> File finding 文件查找
42> File inclusion/exclusion 文件过滤
43
44grep常用操作
45
46```shell
47grep -r 'hello_world' # 简单用法
48grep '^hello_world' . # 简单正则
49ls -l | grep .py # 管道用法
50```
51
52### Searching
53
54简单的文本搜索,默认是递归的。
55
56```
57ack-grep hello
58ack-grep -i hello
59ack-grep -v hello
60ack-grep -w hello
61ack-grep -Q 'hello*'
62```
63
64### Search File
65
66对搜索结果进行处理,比如只显示一个文件的一个匹配项,或者xxx
67
68```shell
69ack-grep --line=1 # 输出所有文件第二行
70ack-grep -l 'hello' # 包含的文件名
71ack-grep -L 'print' # 非包含文件名
72```
73
74### File presentation
75
76输出的结果是以什么方式展示呢,这个部分有几个参数可以练习下
77
78```shell
79ack-grep hello --pager='less -R' # 以less形式展示
80ack-grep hello --noheading # 不在头上显示文件
81ack-grep hello --nocolor # 不对匹配字符着色
82```
83
84### File finding
85没错,它可以查找文件,以省去你要不断的结合find和grep的麻烦,虽然在linux的思想是一个工具做好一件事。
86
87```shell
88ack-grep -f hello.py # 查找全匹配文件
89ack-grep -g hello.py$ # 查找正则匹配文件
90ack-grep -g hello --sort-files # 查找然后排序
91```
92
93### File Inclusion/Exclusion
94
95文件过滤,个人觉得这是一个很不错的功能。如果你曾经在搜索项目源码是不小心命中日志中的某个关键字的话,你会觉得这个有用。
96
97```shell
98ack-grep --python hello # 查找所有python文件
99ack-grep -G hello.py$ hello # 查找匹配正则的文件
100```
101
102## 参考资料
103
104- [ack官网](https://beyondgrep.com/)