我发现grep的——color=always标志非常有用。但是,grep只打印匹配的行(除非您要求上下文行)。假设它打印的每一行都有一个匹配项,那么高亮显示并不能增加尽可能多的功能。
我真的想猫一个文件,并看到整个文件与模式匹配突出显示。
是否有某种方法可以告诉grep打印正在读取的每一行,而不管是否有匹配?我知道我可以编写一个脚本来在文件的每一行上运行grep,但我很好奇标准grep是否可以做到这一点。
我发现grep的——color=always标志非常有用。但是,grep只打印匹配的行(除非您要求上下文行)。假设它打印的每一行都有一个匹配项,那么高亮显示并不能增加尽可能多的功能。
我真的想猫一个文件,并看到整个文件与模式匹配突出显示。
是否有某种方法可以告诉grep打印正在读取的每一行,而不管是否有匹配?我知道我可以编写一个脚本来在文件的每一行上运行grep,但我很好奇标准grep是否可以做到这一点。
当前回答
是否有某种方法可以告诉grep打印正在读取的每一行 不管是否匹配?
Option -C999 will do the trick in the absence of an option to display all context lines. Most other grep variants support this too. However: 1) no output is produced when no match is found and 2) this option has a negative impact on grep's efficiency: when the -C value is large this many lines may have to be temporarily stored in memory for grep to determine which lines of context to display when a match occurs. Note that grep implementations do not load input files but rather reads a few lines or use a sliding window over the input. The "before part" of the context has to be kept in a window (memory) to output the "before" context lines later when a match is found.
模式,如^| pattern或pattern |$或任何空匹配的子模式,如[^ -~]?|PATTERN是一个很好的技巧。然而,1)这些模式不会显示不匹配的行作为上下文突出显示,2)这不能与其他一些grep选项组合使用,例如-F和-w。
所以这些方法都不能让我满意。我使用ugrep,并使用选项-y增强grep,以有效地将所有不匹配的输出显示为彩色高亮的上下文行。其他类似grep的工具,如ag和ripgrep也提供了一个传递选项。但是ugrep与GNU/BSD grep兼容,并提供了grep选项的超集,如-y和-Q。例如,下面是选项-y与-Q结合时显示的内容(输入模式的交互式查询UI):
ugrep -Q -y FILE ...
其他回答
是否有某种方法可以告诉grep打印正在读取的每一行 不管是否匹配?
Option -C999 will do the trick in the absence of an option to display all context lines. Most other grep variants support this too. However: 1) no output is produced when no match is found and 2) this option has a negative impact on grep's efficiency: when the -C value is large this many lines may have to be temporarily stored in memory for grep to determine which lines of context to display when a match occurs. Note that grep implementations do not load input files but rather reads a few lines or use a sliding window over the input. The "before part" of the context has to be kept in a window (memory) to output the "before" context lines later when a match is found.
模式,如^| pattern或pattern |$或任何空匹配的子模式,如[^ -~]?|PATTERN是一个很好的技巧。然而,1)这些模式不会显示不匹配的行作为上下文突出显示,2)这不能与其他一些grep选项组合使用,例如-F和-w。
所以这些方法都不能让我满意。我使用ugrep,并使用选项-y增强grep,以有效地将所有不匹配的输出显示为彩色高亮的上下文行。其他类似grep的工具,如ag和ripgrep也提供了一个传递选项。但是ugrep与GNU/BSD grep兼容,并提供了grep选项的超集,如-y和-Q。例如,下面是选项-y与-Q结合时显示的内容(输入模式的交互式查询UI):
ugrep -Q -y FILE ...
这里有一些类似的东西。很有可能,无论如何你都会用得更少,所以试试这个:
less -p pattern file
它将突出显示该模式,并跳转到该模式在文件中第一次出现的位置。
你可以用n跳到下一个出现点,用p跳到前一个出现点,用q退出。
也试一试:
exception 'pattern1|pattern2' FILE.txt | less -Sp 'pattern1|pattern2'
这将为您提供一个带有高亮模式/s的表格输出。
你可以从https://github.com/kepkin/dev-shell-essentials上使用我的高亮脚本
它比grep更好,因为您可以用自己的颜色突出显示每个匹配。
$ command_here | highlight green "input" | highlight red "output"
sed方式
由于已经有很多不同的解,但没有一个把sed表示为解, 而且由于sed比grep更轻、更快,我更喜欢使用sed来完成这种工作:
sed 's/pattern/\o33[47;31;1m&\o033[0m/' file
这似乎不太直观。
\o33是用于生成八进制字符033 ->转义的sed语法。 (一些shell和编辑器还允许输入<Ctrl>-<V>后面跟着<Esc>,直接输入字符。) Esc [47;31日;1m是ANSI转义码:背景灰色,前景红色,加粗。 &将重新打印图案。 Esc [0 m返回默认颜色。
你也可以高亮整行,但把模式标记为红色:
sed -E <file -e \
's/^(.*)(pattern)(.*)/\o33[30;47m\1\o33[31;1m\2\o33[0;30;47m\3\o33[0m/'
动态尾部-f,紧跟日志文件
使用sed的一个好处是:您可以在控制台上发送警报哔哔声,使用bell ascii字符0x7。我经常像这样使用sed:
sudo tail -f /var/log/kern.log |
sed -ue 's/[lL]ink .*\([uU]p\|[dD]own\)/\o33[47;31;1m&\o33[0m\o7/'
-u代表无缓冲。所以线条会立即处理。
所以当我连接或断开以太网电缆时,我会听到一些哔哔声。
当然,与链接模式不同的是,您可以在同一个文件中查看USB,甚至在一些/var/log/mail.log中搜索from=…