Convince grep to output all lines, not just those with matches
grep --color -E "test|$" yourfile
What we're doing here is matching against the $
pattern and the test pattern, obviously $
doesn't have anything to colourize so only the test pattern gets color. The -E
just turns on extended regex matching.
You can create a function out of it easily like this:
highlight () { grep --color -E "$1|$" "${@:1}" ; }
ack --passthru --color string file
for Ubuntu and Debian, use ack-grep instead of ack
ack-grep --passthru --color string file
Another way to do this properly and portably with grep
(besides using two regexes with alternation as in the accepted answer) is via the null pattern (and respectively null string).
It should work equally well with both -E
and -F
switches since, per the standard:
-E
Match using extended regular expressions.
[...] A null ERE shall match every line.
and
-F
Match using fixed strings.
[...] A null string shall match every line.
So it's simply a matter of running
grep -E -e '' -e 'pattern' infile
and respectively
grep -F -e '' -e 'string' infile