2017-07-03-grep工具
Table of Contents
1 grep
刚开始使用linux的时候就了解了grep了。基本天天都要用。和很多linux下的 工具一样,做得事情很简单,但实用。
总结一些常用的选项功能:
-i:忽略大小写。-R:递归地搜索目录。-e:如果需要搜索多个pattern,可以使用多个-e选项。这样 每个pattern满足都会找出来。另外,它还适合以-开头的pattern。-A:显示满足条件行的下面几行。比如grep -A20显示下面20行。-B:显示满足条件行的上面几行。比如grep -B20显示上面20行。-C:显示满足条件行的上面和下面几行。比如grep -C20显示上面 20行和下面20行。-n:显示引号。-l:加上该选项,结果只会显示包含的pattern的文件名,不会再 显示对应行的内容。
2 ag(the_silver_searcher)
遇然情况下知道了ag,试用一下,感觉比grep快好多啊。于是Emacs中也换成 ag。
- 它可以根据
.gitignore等文件过滤掉一些无关的文件。 - 使用
Pthread来并行搜索,发挥CPU的全部实力。 - 一个字母一个字母搜索的时候,使用 BM算法 来加速。
- 使用 PCRE's JIT 来操作正则。
- 在把每个相同的正则应用到每个文件时,ag会先调用
pcre_study()函 数。 - 不会对ignore的文件调用
fnmatch()函数,非正则的pattern会直 接载入到数组中进行二进制搜索。
ag的正则支持multiline搜索。
ag可以忽略一些目录,开始时使用 .agignore 文件。 2.0.0 以后,该名
字改为 .ignore 。写法和 .gitignore 是一样的。
3 rg(ripgrep)
这是我最后了解到的工具。
快的原因:
It is built on top of Rust's regex engine. Rust's regex engine uses
finite automata, SIMD and aggressive literal optimizations to make
searching very fast.
Rust's regex library maintains performance with full Unicode support
by building UTF-8 decoding directly into its deterministic finite
automaton engine.
It supports searching with either memory maps or by searching
incrementally with an intermediate buffer. The former is better for
single files and the latter is better for large directories. ripgrep
chooses the best searching strategy for you automatically.
Applies your ignore patterns in .gitignore files using a
RegexSet. That means a single file path can be matched against
multiple glob patterns simultaneously.
It uses a lock-free parallel recursive directory iterator, courtesy of
crossbeam and ignore.
实际使用时,我需要rg忽略某些目录。可以使用 .rgignore 文件来指定。
比如在一个git库的根目录下新建一个 .rgignore 。按照 .gitignore 随
便写点就可以了。
4 ack
这个工具只是知道,没有用过。据说如果没有ack就没有 ag 。它是用纯perl 来做的。