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?
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
.
– cuonglm
Jul 23 '14 at 10:38
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.
– Stéphane Chazelas
Jul 23 '14 at 10:50
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.
– cuonglm
Jul 23 '14 at 10:54
grep -e X -e '^'
works but grep -e '^' -e X
like your answer does not.
– cuonglm
Jul 23 '14 at 10:55
grep --color -e x -e '^'
segfaults in OS/X (FreeBSD grep 2.5.1) but not with $
, so I've updated the answer once again for better portability.
– Stéphane Chazelas
Jul 23 '14 at 12:09
grep --color -e x -e '^'
cause a segfaults?
– cuonglm
Jul 24 '14 at 15:22
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
– Valentin Bajrami Jul 23 '14 at 10:23man grep
, the answer is hard to miss even if you put in minimal effort – Anthon Jul 23 '14 at 11:01