Monday, February 14, 2011

Highlighting Standard Output in color

I trace many of my log files to the screen, things some times get overlooked when there is a bunch of output all at one time. So after googling a bit I found a nice little gem, I will try to find a reference to link to so I can give credit where it is due.


#!/bin/bash
# tputcolors

TPUT=$(which tput)
if [[ -n $TPUT && -x $TPUT ]] ; then
R=`tput setaf 1; tput bold`
G=`tput setaf 2; tput bold`
Y=`tput setaf 3; tput bold`
X=`tput sgr0`
P=`tput setaf 5; tput bold`
else
R=`echo -en "\\033[1;31m"`
G=`echo -en "\\033[1;32m"`
Y=`echo -en "\\033[1;33m"`
X=`echo -en "\\033[0;39m"`
P=`echo -en "\\033[0;35m"`
fi

while read line ; do
case "$line" in
*[Ee]rror*|*problem*|*ERROR*|*E/*)
echo -n "${R}"
;;
*[Ww]arning*|*WARNING*|*W/*)
echo -n "${Y}"
;;
*SUCCESS*|*SUCCESSFUL*|*D/*)
echo -n "${G}"
;;
*INFO*|*I/*)
echo -n "${P}"
;;
esac
echo -e "$line${X}"
done


Labels

android (2) debugging (1) fedora (1) linux (1) subversion (1) ubuntu (1) ui (1) vim (1)