29/5/09

Highlight text from a command's stdout

It happened to me many times. For example when reading the output of tcpdum and wanting to look at an especific IP but without cutting the context I used to do "| grep --color=auto -C 10 IP".
Or when reading the output of strace and looking for a written file the context is important, so I came up with this:

highlight (){
    if [ -n "$1" ] ; then
        sed "s/$1/\x1b[32;1m&\x1b[1;0m/g" /dev/stdin
    else
        echo "ERROR: What words to highlight?"
    fi

}

Just write it in your .basrc.

For example:
cat .xsession-errors | highlight gnome-panel
Will highlight only "gnome-panel" in green, leaving the output intact.

Some Vista fun...