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.

 

 


Posted by xxv (193.253.xx.xx) on Mon 6 Nov 2006 at 15:03
[ 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
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
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
[ 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 (128.135.xx.xx) on Sun 27 May 2012 at 01:19
sweet, works on archlinux too. rxvt-unicode

[ 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
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 (54.240.xx.xx) on Mon 15 Jul 2013 at 19:15
Thanks! Very helpful.

[ 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 (109.205.xx.xx) on Tue 29 Apr 2014 at 12:57
Just for the record, this may be achieved quite easily.
Simply do the grep without color on the whole phrase, and pipe it to the colored grep which will highlight the IP only.

[ 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 ]

Posted by Anonymous (152.62.xx.xx) on Wed 29 Dec 2010 at 11:47
To show multiple colors in one command, please use:

$ echo 'RED GREEN BLUE' | grep RED --color=always | { export GREP_COLORS='ms=01;32:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=3 6'; grep GREEN; }

This is just an sample.
In daily use, we may define aliases / script for shortcut.

[ Parent | Reply to this comment ]

Posted by Anonymous (209.119.xx.xx) on Wed 1 Aug 2012 at 04:12
Sorry this does not work for me . I receive only one color : echo 'RED GREEN BLUE' | grep RED --color=always | { export GREP_COLORS='ms=01;32:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=3 6'; grep GREEN; } ________Regards,_______ ds_biswas@yahoo.com

[ Parent | Reply to this comment ]

Posted by VoiceX (62.2.xx.xx) on Mon 16 May 2011 at 16:22
how can I highlight, if I use grep 2 times?

example:
find . -iname "*.html" -exec grep --color=auto -HinE "word1" {} \; | grep --color=auto "word2"

in the ex above, only word2 will be highlighted :(

[ Parent | Reply to this comment ]

Posted by Anonymous (2001:0xx:0xx:0xxx:0xxx:0xxx:xx) on Tue 17 May 2011 at 01:50
good question... that's what i wanna know! grep should just let you specify more than one search term, or use booleans so you don't have to do so much piping!! and it should highlight all the different terms in different colors!!

[ Parent | Reply to this comment ]

Posted by Anonymous (24.6.xx.xx) on Sun 5 Jun 2011 at 02:37
For multiple patterns with one grep, use "egrep" (grep with extended regular expressions) with syntax like this:

# egrep "(pattern1|pattern2|...)" somefile

You would rewrite your example above like this (only changes the last grep to egrep):

# find . -iname "*.html" -exec grep --color=auto -HinE "word1" {} \; | egrep --color=auto "(word1|word2)"

If you wanted lines with _either_ word in it, change the first grep and omit the second:

# find . -iname "*.html" -exec egrep --color=auto -HinE "(word1|word2)" {} \;

The highlighting for matched text is all in one color, though see the man page for more stuff you can do with the GREP_COLORS envvar. With it, you can set different colors for different contextual things, like the color for the rest of the line that has the match ("cx="), but isn't the matching text itself. Also, grep will color filenames, line-numbers, byte-offsets, etc... like when you give grep multiple files to search:

# egrep --color=always -i "(yes|true)" /etc/default/*

[ Parent | Reply to this comment ]

Posted by Anonymous (24.6.xx.xx) on Sun 5 Jun 2011 at 02:45
oops... I meant "sl=" instead of "cx=" above. I haven't tried the "cx=" option yet, which colors "context" lines, where "sl=" colors "selected" lines. I've set the following in my .bashrc which is similar to the default (red for matches), but makes the rest of the line yellow:

export GREP_COLORS="ms=01;31:mc=01;31:sl=01;33:cx=:fn=35:ln=32:bn=3 2:se=36"

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 330 votes ~ 1 comments )

 

 

Related Links