How to color grep output

When I type

memcached -vv 2>&1 | grep --color=always "SET\|GET"

I get nice memcached stats

GET AR-City-1

GET rest-part-241

SET rest-part-241 Value len is 685

GET main-rest-list-1

It would be great to pipe this to something, and color GET in green instead of the default red.

How could I do that?


This could be probably written shorter but solution gives the result:

memcached -vv 2>&1 | sed -e "s/^GET.*$/\x1b[31m&\x1b[0m/" | sed -e "s/^SET.*$/\x1b[32m&\x1b[0m/"

Or if you wish with background colors instead of foreground color:

memcached -vv 2>&1 | sed -e "s/^GET.*$/\x1b[41m&\x1b[0m/" | sed -e "s/^SET.*$/\x1b[42m&\x1b[0m/"

So GET is green, SET is red.


for gnu grep this will work:

$ export GREP_COLOR="01;32"

where "01" means: bold and "32" green. The default is "01:31" (bold red). Other colors:

  • 31:red
  • 32:green
  • 33:yellow
  • 34:blue
  • 35:purple

These colors may look different depending on how your terminal is configured, but those above are the standard colors.

If

$ env | grep GREP_COLORS

gives you a result, those settings supersede the GREP_COLOR setting (note the "S"), see the grep manage for more detailed settings using GREP_COLORS.


try rpen a text highlighter based on egrep :)