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.

Share/Save/Bookmark


Posted by xxv (193.253.xx.xx) on Mon 6 Nov 2006 at 15:03
[ Send Message | View xxv's Scratchpad ]
That's incredibly handy. I generally use less to do that sort of thing, but if grep can do it - that's wonderful. Thanks for the tip!

[ Parent | Reply to this comment ]

Posted by dgerman (98.109.xx.xx) on Wed 3 Jun 2009 at 20:00
[ Send Message ]
export GREP_COLOR=7
displays matches in inverse video, very easy to see.

[ Parent | Reply to this comment ]

Posted by Anonymous (198.125.xx.xx) on Mon 6 Nov 2006 at 15:03
that's a handy simple tip - thx!

[ Parent | Reply to this comment ]

Posted by Anonymous (84.133.xx.xx) on Mon 6 Nov 2006 at 15:11
Everytime I return from Gentoo to Debian, I am happy that Debian does not make everything eyecancerish colorfull...

[ Parent | Reply to this comment ]

Posted by TRx (80.32.xx.xx) on Thu 9 Nov 2006 at 17:53
[ Send Message ]
Then Gentoo can make you happy too. Just add NOCOLOR="true" in /etc/make.conf and portage won't hurt your eyes anymore. :)

You can achieve the same behaviour in equery (and other gentoolkit apps) with the "--nocolor" parameter.

regards

[ Parent | Reply to this comment ]

Posted by alfadir (141.58.xx.xx) on Mon 6 Nov 2006 at 16:57
[ Send Message | View Weblogs ]

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 ]

Posted by Anonymous (24.79.xx.xx) on Mon 6 Nov 2006 at 21:47
This is cool, but when coding I typically pipe grep output into less -- which causes the color to disappear. Is there a solution to this?

[ Parent | Reply to this comment ]

Posted by JoshTriplett (66.93.xx.xx) on Mon 6 Nov 2006 at 22:54
[ Send Message ]
I do the same thing. Solving this requires two steps:
  1. 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".
  2. 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 ]

Posted by Anonymous (81.247.xx.xx) on Thu 9 Nov 2006 at 20:27
You can use "glark" (there is a debian package).
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 ]

Posted by Anonymous (66.93.xx.xx) on Sat 11 Nov 2006 at 10:54
# glark printf /usr/share/doc/glark/README.Debian | less -r
In 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 ]

Posted by Anonymous (208.247.xx.xx) on Tue 7 Nov 2006 at 16:52
Use the --color option to display matches in the input in color (red, by default). Color is added via ANSI escape sequences, which don't work in all displays, but grep is smart enough to detect this and won't use color (even if specified) if you're sending the output down a pipeline. Otherwise, if you piped the output to (say) less, the ANSI escape sequences would send garbage to the screen. If, on the other hand, that's really what you want to do, there's a workaround: use the --color=always to force it, and call less with the -R flag (which prints all raw control characters). That way, the color codes will escape correctly and you'll page through screens of text with your matched patterns in full color:

grep --color=always "regexp" myfile | less -R

[ Parent | Reply to this comment ]

Posted by Anonymous (83.130.xx.xx) on Sat 11 Nov 2006 at 10:50
This trick's great!
Everyone at my work now use it :)

[ Parent | Reply to this comment ]

Posted by Anonymous (12.216.xx.xx) on Wed 29 Nov 2006 at 19:36
That's great! What would be even more cool is if you could do:
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 ]

Posted by Anonymous (84.108.xx.xx) on Sat 19 Sep 2009 at 20:18
hey,
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 ]

Posted by Anonymous (84.60.xx.xx) on Wed 17 Oct 2007 at 10:13
I use it regularly with gentoo, and just searched for it to apply it on my debian systems. just thank you very much!

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Related Links

Quick Site Search