grep: highlighting matches in color
Posted by JoshTriplett on Mon 6 Nov 2006 at 09:48
Many Debian users use grep regularly. But did you know that grep can highlight the text it matches in color?
Much like ls, grep supports the use of color in its output, controlled by a --color option. By default, grep outputs matching lines, but does not indicate what portion of the line matched. With --color=auto, grep will highlight the matching portion of the line in color when it outputs to a terminal, while avoiding the use of any potentially-harmful terminal control sequences when standard output goes somewhere else (like a file or pipe). If you like the colorful grep, you can simply export GREP_OPTIONS='--color=auto' in your shell startup script.
grep's default highlight uses a red color. You can change this color by setting the GREP_COLOR environment variable to a different escape sequence fragment. I use export GREP_COLOR='1;32', which produces a bright green.
[ Parent | Reply to this comment ]
GREP_COLOR=7displays matches in inverse video, very easy to see.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
You can achieve the same behaviour in equery (and other gentoolkit apps) with the "--nocolor" parameter.
regards
[ Parent | Reply to this comment ]
Cool.
Using the 256 color enabled Eterm one can use :
export GREP_COLOR='00;38;5;226'
to get a nice yellow color.
If you rather like light green try
export GREP_COLOR='00;38;5;157'
Background is set with 48;5 if you want to mix the two.. .. well your eyes may bleed, but anyway..
export GREP_COLOR='00;38;5;157;48;5;226'
Works under Eterm and should work with xterm-256.
The same thing can be used with .dircolors
.test 00;38;5;157;48;5;226
If one has time one can make nicer paletts and colorschemes with 256 colors than just the normal 8-16 colors that are normaly very strong and hurts your eyes.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
- To make grep print color even into a pipe, you need the option --color=always. *Don't* put this option into GREP_OPTIONS, unless you want to unleash all manner of insanity and brokenness. Instead, if you don't want to always type grep --color=always, you might make an alias or shell function "cgrep".
- To make less interpret color escape sequences, you need the -R option. export LESS=-R in your shell startup script.
[ Parent | Reply to this comment ]
This is a ruby script (as far as know), it's some kind of grep replacement with full color support.
I don't use it too often because I find it very slow compared to grep but when highlight is a must, I use it.
To answer your question, you can use "less" with glark and keep the color support.
For example:
# glark printf /usr/share/doc/glark/README.Debian | less -r
D-fence
[ Parent | Reply to this comment ]
# glark printf /usr/share/doc/glark/README.Debian | less -rIn general, you don't want to use less -r, which allows arbitrary control characters through to affect the terminal. Instead, use less -R, which only allows color escape sequences through. I set LESS=-R in my shell startup script to always allow this.
[ Parent | Reply to this comment ]
grep --color=always "regexp" myfile | less -R
[ Parent | Reply to this comment ]
Everyone at my work now use it :)
[ Parent | Reply to this comment ]
egrep --color 'SPAM because your mail server IP address ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) was found in (dnsbl.sorbs.net)' /var/log/maillog
and have $1 and $2 colored slightly differently from the rest of the match.
with that particular regex, all I care about is the IP address, but nearly every log-file line has an IP address, so I need to make the regex more specific. So how would you like
'SPAM because your mail server IP address ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) was found in (dnsbl.sorbs.net|sbl.spamhaus.org)'
where the IP address and dnsbl that blocked the mail were both highlighted, slightly differently? I'm sure it would be useful for other stuff too.
I realize it would be tricky, because, what would happen if you did:
(\d+\.(\d+\.(\d+\.(\d+)))\w)
Still, I'm sure it could be worked out.
[ Parent | Reply to this comment ]
i'm also looking for a synopsis to color 2 patterns in 2 different colors.
where you able to achive this ?
i'll appreciate the reply.
oren
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]