Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
If you want to use sed I think you can use sed one time and you can skip using of grep: `sed -n -r "/SET|GET/ s@SET...@replace_what_you_want@p ; s@GET...@replace_what_you_want@p" –  uzsolt Nov 18 '11 at 15:39
    
I worked on this a little, this is solution: memcached -vv 2>&1 | sed -e "s/GET.*/\x1b[32m&\x1b[0m/" | sed -e "s/SET.*/\x1b[31m&\x1b[0m/" | grep "GET\|SET" # thx! –  dux Nov 18 '11 at 17:22
add comment

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.

share|improve this answer
    
thx for the answer, settings works well in a way that default color now is green, but I do not have SET in red and GET in green as sad in question. –  dux Nov 18 '11 at 16:42
add comment

If you want to see different words in different colors then use colorize for example

share|improve this answer
add comment

Based on trurl answer:

memcached -vv 2>&1 | GREP_COLOR='01;31' grep --color=always SET \
                   | GREP_COLOR='01;32' grep --color=always GET
share|improve this answer
    
I work in GIT bash shell under Win32 and ssh connect to Ubuntu. This desn't work. B-scan hack with sed works well –  dux Nov 18 '11 at 17:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.