Linux – 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?

Best Answer

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.