I find grep
\'s --color=always
flag to be tremendously useful. However, grep only prints lines with matches (unless you ask for context lines). Give
Is there some way I can tell grep to print every line being read regardless of whether there's a match?
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.
A pattern such as ^|PATTERN
or PATTERN|$
or any empty-matching sub-pattern for that matter such as [^ -~]?|PATTERN
is a nice trick. However, 1) these patterns don't show non-matching lines highlighted as context and 2) this can't be used in combination with some other grep options, such as -F
and -w
for example.
So none of these approaches are satisfying to me. I'm using ugrep, and enhanced grep with option -y
to efficiently display all non-matching output as color-highlighted context lines. Other grep-like tools such as ag and ripgrep also offer a pass-through option. But ugrep is compatible with GNU/BSD grep and offers a superset of grep options like -y
and -Q
. For example, here is what option -y
shows when combined with -Q
(interactive query UI to enter patterns):
ugrep -Q -y FILE ...
Here are some ways to do it:
grep --color 'pattern\|$' file
grep --color -E 'pattern|$' file
egrep --color 'pattern|$' file
The |
symbol is the OR operator. Either escape it using \
or tell grep that the search text has to be interpreted as regular expressions by adding -E or using the egrep
command instead of grep
.
The search text "pattern|$" is actually a trick, it will match lines that have pattern
OR lines that have an end. Because all lines have an end, all lines are matched, but the end of a line isn't actually any characters, so it won't be colored.
One other answer mentioned grep's -Cn switch which includes n lines of Context. I sometimes do this with n=99 as a quick-and-dirty way of getting [at least] a screenfull of context when the egrep pattern seems too fiddly, or when I'm on a machine on which I've not installed rcg and/or ccze.
I recently discovered ccze
which is a more powerful colorizer. My only complaint is that it is screen-oriented (like less
, which I never use for that reason) unless you specify the -A switch for "raw ANSI" output.
+1 for the rcg
mention above. It is still my favorite since it is so simple to customize in an alias. Something like this is usually in my ~/.bashrc:
alias tailc='tail -f /my/app/log/file | rcg send "BOLD GREEN" receive "CYAN" error "RED"'
Use colout
program: http://nojhan.github.io/colout/
It is designed to add color highlights to a text stream. Given a regex and a color (e.g. "red"), it reproduces a text stream with matches highlighted. e.g:
# cat logfile but highlight instances of 'ERROR' in red
colout ERROR red <logfile
You can chain multiple invocations to add multiple different color highlights:
tail -f /var/log/nginx/access.log | \
colout ' 5\d\d ' red | \
colout ' 4\d\d ' yellow | \
colout ' 3\d\d ' cyan | \
colout ' 2\d\d ' green
Or you can achieve the same thing by using a regex with N groups (parenthesised parts of the regex), followed by a comma separated list of N colors.
vagrant status | \
colout \
'\''(^.+ running)|(^.+suspended)|(^.+not running)'\'' \
green,yellow,red
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.
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.
You can jump to the next occurence with n
and to the previous occurence with p
. Quit with q
.