我发现grep的——color=always标志非常有用。但是,grep只打印匹配的行(除非您要求上下文行)。假设它打印的每一行都有一个匹配项,那么高亮显示并不能增加尽可能多的功能。

我真的想猫一个文件,并看到整个文件与模式匹配突出显示。

是否有某种方法可以告诉grep打印正在读取的每一行,而不管是否有匹配?我知道我可以编写一个脚本来在文件的每一行上运行grep,但我很好奇标准grep是否可以做到这一点。


当前回答

这里有一些方法:

grep --color 'pattern\|$' file
grep --color -E 'pattern|$' file
egrep --color 'pattern|$' file

|符号是OR运算符。要么使用\转义它,要么通过添加-E或使用egrep命令而不是grep告诉grep搜索文本必须解释为正则表达式。

搜索文本“pattern|$”实际上是一个技巧,它将匹配具有模式的行或具有结束的行。因为所有的行都有一个结尾,所有的行都被匹配,但是一行的结尾实际上不是任何字符,所以它不会被着色。

要通过管道传递有颜色的部分,例如向less传递,请向——color提供always参数:

grep --color=always 'pattern\|$' file | less -r
grep --color=always -E 'pattern|$' file | less -r
egrep --color=always 'pattern|$' file | less -r

其他回答

以下是我的方法,灵感来自@kepkin的解决方案:

# Adds ANSI colors to matched terms, similar to grep --color but without
# filtering unmatched lines. Example:
#   noisy_command | highlight ERROR INFO
#
# Each argument is passed into sed as a matching pattern and matches are
# colored. Multiple arguments will use separate colors.
#
# Inspired by https://stackoverflow.com/a/25357856
highlight() {
  # color cycles from 0-5, (shifted 31-36), i.e. r,g,y,b,m,c
  local color=0 patterns=()
  for term in "$@"; do
    patterns+=("$(printf 's|%s|\e[%sm\\0\e[0m|g' "${term//|/\\|}" "$(( color+31 ))")")
    color=$(( (color+1) % 6 ))
  done
  sed -f <(printf '%s\n' "${patterns[@]}")
}

这接受多个参数(但不允许自定义颜色)。例子:

$ noisy_command | highlight ERROR WARN

这是一种方法,

wc -l filename

会给你行数,比如NN,你能做什么

grep -C NN --color=always filename

这里有一些类似的东西。很有可能,无论如何你都会用得更少,所以试试这个:

less -p pattern file

它将突出显示该模式,并跳转到该模式在文件中第一次出现的位置。

你可以用n跳到下一个出现点,用p跳到前一个出现点,用q退出。

是否有某种方法可以告诉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 ...

我把这个添加到我的.bash_aliases中:

highlight() {
  grep --color -E "$1|\$"
}