EDIT:
This works with OS X Mountain Lion's grep:
grep --color -E 'pattern1|pattern2|$'
This is better than '^|pattern1|pattern2'
because the ^
part of the alternation matches at the beginning of the line whereas the $
matches at the end of the line. Some regular expression engines won't highlight pattern1
or pattern2
because ^
already matched and the engine is eager.
Something similar happens for 'pattern1|pattern2|'
because the regex engine notices the empty alternation at the end of the pattern string matches the beginning of the subject string.
[1]: http://www.regular-expressions.info/engine.html
FIRST EDIT:
I ended up using perl:
perl -pe 's:pattern:\033[31;1m$&\033[30;0m:g'
This assumes you have an ANSI-compatible terminal.
ORIGINAL ANSWER:
If you're stuck with a strange grep
, this might work:
grep -E --color=always -A500 -B500 'pattern1|pattern2' | grep -v '^--'
Adjust the numbers to get all the lines you want.
The second grep
just removes extraneous --
lines inserted by the BSD-style grep
on Mac OS X Mountain Lion, even when the context of consecutive matches overlap.
I thought GNU grep omitted the --
lines when context overlaps, but it's been awhile so maybe I remember wrong.