Sunday, April 17, 2011

Highlighting Command Output with Generic Colouriser

I recently started using Generic Colouriser, which is a tool that has the ability to colour the output from different programs based on regular expressions. It comes with config files for colouring output from commands such as diff, traceroute, ping, netstat and cvs, but it is also very easy to colour your own commands.

I wrote a config for ps which greys out the processes being run by root and highlights in green, the processes being run by me:

# configuration file for ps
# grey everything out
regexp=^[a-zA-Z]+ .*$
colours=bold black
======
# highlight my processes in green
regexp=^sharfah.*$
colours=green
You can then alias ps to use grc:
alias ps='grc ps'
I've also changed the log config, so that it greys out debug statements, colours warnings yellow and errors red. Most of my logs are produced by log4j. Here is my config:
# this configuration file is suitable for displaying log files
#errors
regexp=^.*(ERROR|Error|Exception).*$
colours=bold red
======
#stack trace
regexp=^\s+at [^:]*:\d*\)$
colours=red
======
regexp=^.*(FATAL|Fatal).*$
colours=on_red
======
regexp=^.*(WARNING|Warning).*$
colours=bold yellow
======
regexp=^.*(DEBUG|Debug).*$
colours=bold black
======
# this is a time
regexp=[^\d]*\d\d:\d\d:\d\d[\.,]{0,1}\d{0,1}\d{0,1}\d{0,1}
colours=bold blue
count=once
======
# ip number
regexp=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
colours=bold magenta
count=more
I then created a bash function called cless which I use instead of less, to view my logs. I've also aliased tail.
cless ()
{
    if [ $# -eq 0 ]; then
        echo Usage: cless FILE;
        return 1;
    fi;
    grc cat $1 | less -iMF -R
}
alias tail='grc tail'
If you are getting bored of your bland terminal, try this out!

No comments:

Post a Comment