相关疑难解决方法(0)

说服 grep 输出所有行,而不仅仅是那些匹配的行

假设我有以下文件:

$ cat test

test line 1
test line 2
line without the search word
another line without it
test line 3 with two test words
test line 4
Run Code Online (Sandbox Code Playgroud)

默认情况下,grep返回包含搜索词的每一行:

$ grep test test

test line 1
test line 2
test line 3 with two test words
test line 4
Run Code Online (Sandbox Code Playgroud)

传递--color参数 togrep将使其突出显示与搜索表达式匹配的行部分,但它仍然只返回包含该表达式的行。有没有办法grep输出源文件中的每一行,但突出显示匹配项?

我目前实现这一点的可怕黑客(至少在没有 10000+ 连续行且没有匹配项的文件上)是:

$ grep -B 9999 -A 9999 test test
Run Code Online (Sandbox Code Playgroud)

两个命令的截图

如果grep不能做到这一点,是否有另一个提供相同功能的命令行工具?我摆弄过ack,但它似乎也没有选择。

grep highlighting

140
推荐指数
5
解决办法
6万
查看次数

标签 统计

grep ×1

highlighting ×1