The grep
command accepts a --color=always
option, so you can use
grep 'keyword1' file.log --color=always | grep 'keyword2'
As gertvdijk points out, this command may be inefficient, but it will look for all lines that contain both keyword1 and keyword2. If you want to highlight them in different colors, you can use
grep 'keyword1' file.log --color=always | GREP_COLORS="mt=01;34" grep --color=always 'keyword2'
which will highlight keyword2 in blue. The mt
part means that grep
will highlight matching text using this CSI code, and 01;34
means "bold blue foreground on normal background".
grep -R --color=always keyword1 . | grep keyword2
should work. Are you redirecting the output again (such as by piping the secondgrep
intoless -R
)? If so, you'll need to add--color=always
to the second one also. – Daniel H Nov 14 '13 at 03:55--color=auto
would be needed for the secondgrep
, just after'keyword2'
. – Luis May 21 '19 at 10:34--color=always
to the answer, because you might want to pipe it toless
or something also. – Daniel H May 21 '19 at 20:59less
, but I just tried and, for some reason, it messes up the format, withESC
codes and things o_O Anyway, thank you for updating the answer! – Luis May 22 '19 at 10:15less
doesn't interpret escape codes. You need to pass-R
to get it to do that. – Daniel H May 22 '19 at 17:28