Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
    
Related question from the U&L SE: How to have tail -f show colored output – Cristian Ciupitu Jun 22 '14 at 23:34
up vote 6 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

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

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

share|improve this answer

try rpen a text highlighter based on egrep :)

share|improve this answer
5  
Always try to add more details in your answer. – Renju Chandran chingath May 6 '14 at 10:01
1  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Ƭᴇcʜιᴇ007 May 6 '14 at 13:54

Another alternative text colorizer based on ack-grep:

https://github.com/paoloantinori/hhighlighter

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .