viewing the entire file with highlighted matches
I find grep
's --color=always
flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Given that each line it prints has a match, the highlighting doesn't add as much capability as it could.
I'd really like to cat
a file and see the entire file with the pattern matches highlighted.
Is there some way I can tell grep to print every line being read regardless of whether there's a match? I know I could write a script to run grep on every line of a file, but I was curious whether this was possible with standard grep
.
这里有一些方法来做到这一点:
grep --color -E 'pattern|$' file
grep --color 'pattern|$' file
egrep --color 'pattern|$' file
Here's something along the same lines. Chances are, you'll be using less anyway, so try this:
less -p pattern file
It will highlight the pattern and jump to the first occurrence of it in the file.
I'd like to recommend ack -- better than grep, a power search tool for programmers.
$ ack --color --passthru --pager="${PAGER:-less -R}" pattern files
$ ack --color --passthru pattern files | less -R
$ export ACK_PAGER_COLOR="${PAGER:-less -R}" $ ack --passthru pattern files
I love it because it defaults to recursive searching of directories (and does so much smarter than grep -r
), supports full Perl regular expressions (rather than the POSIXish regex(3)
), and has a much nicer context display when searching many files.
上一篇: 我怎样才能让grep打印每条匹配行下面和上面的行?
下一篇: 用突出显示的匹配查看整个文件