我如何在每一条匹配的线周围显示前面和后面的5条线?
当前回答
如果您经常搜索代码,AG银搜索器比grep更高效(即更快)。
使用-C选项显示上下文行。
Eg:
ag -C 3 "foo" myFile
line 1
line 2
line 3
line that has "foo"
line 5
line 6
line 7
其他回答
如果您经常搜索代码,AG银搜索器比grep更高效(即更快)。
使用-C选项显示上下文行。
Eg:
ag -C 3 "foo" myFile
line 1
line 2
line 3
line that has "foo"
line 5
line 6
line 7
我采用紧凑的方式:
grep -5 string file
这相当于:
grep -A 5 -B 5 string file
Grep有一个名为Context Line Control的选项,您可以使用--Context,
| grep -C 5
or
| grep -5
应该会成功的
使用grep
$ grep --help | grep -i context
Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context
-C, --context=NUM print NUM lines of output context
-NUM same as --context=NUM
ack使用与grep类似的参数,并接受-C。但它通常更适合于搜索代码。