如何使grep突出显示关键字

默认情况下,grep 不会突出显示关键字或者搜索词。
如果关键字隐藏在单词之间,则使 grep 突出显示搜索词可以使关键字的可见性更加明显。

grep 突出显示关键字的参数是“--color”。

#start of somefile
the big brown fox jumped over the lazy dog
the big brown dog jumped over the lazy fox
the brown fox jumped over the big lazy dog
#end of somefile

例如

grep --color -i dog somefile

会显示:

the big brown fox jumped over the lazy dog
the big brown dog jumped over the lazy fox
the brown fox jumped over the big lazy dog

要使突出显示功能永久化,请在 .bash_profile 或者 .bashrc 中做一个别名:

alias grep='grep --color'

此外,要更改 grep 的颜色,请使用“export”将术语变量 GREP_COLOR 设置为相应的颜色。

例如,使用红色,设置:

export GREP_COLOR=31

要使颜色永久,请将其放入 .bashrc 或者 .bash_profile。

至于其他颜色,请参考下面的转义序列:

标准属性

0 Reset all attributes
1 Bright
2 Dim
4 Underscore
5 Blink
7 Reverse
8 Hidden

前景色

30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White

背景颜色

40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
日期:2020-06-02 22:17:01 来源:oir作者:oir