How can I use grep
command to display both matched and unmatched lines? Matched line should be in red and other lines should be in normal color.
Is there a grep
option available to do that?
grep --color=always -e pattern -e '$'
$TERM
is vt100
.
grep
doesn't use $TERM
, it hardcodes the ANSI escape sequences (regardless of whether your terminal supports (or claims to support) them or not). If grep 'x\|$'
works, so should grep -e X -e '^'
though. Also, check your environment for GREP_COLOR
or GREP_COLORS
variables.
grep 'x\|$'
works but grep -e X -e '^'
, GREP_COLOR
and GREP_COLORS
both are null. I'm on Windows machine, using Secure Shell client
to connect to my Linux Server.
grep -e X -e '^'
works but grep -e '^' -e X
like your answer does not.
If you want to match other lines to have some context, you can use the -A
and -B
options:
grep --color=always -A 9 -B 9 -e pattern
will give you 9 lines of context. If this is not sufficient, you can increase this value.
grep "pattern" filename --color=always
man grep
, the answer is hard to miss even if you put in minimal effort