Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply formatting to unix shell

Tags:

logging

tail

I've been looking at some server logs using tail -f recently, and have thought that it'd be much easier to see some things if I could format the output. Really all I'm looking for is a way to perhaps colour certain words (determined by a regex), and perhaps remove certain words (again, determined by a regex).

I know there's programs which visualize server logs in real time and whatnot, but I'm more interested in this.

like image 870
nickf Avatar asked Feb 04 '09 05:02

nickf


1 Answers

Pipe the output of tail -f into sed, and add in some ANSI escape codes. For example, the following will colorize all numbers in red (color 31) and all quoted strings in bright yellow (color 93):

RED=`echo -en '\e[31m'`
YELLOW=`echo -en '\e[93m'`
RESET=`echo -en '\e[00m'`
tail -f file | sed -E "s/([0-9]+)/$RED\1$RESET/g;s/(\"[^\"]*\")/$YELLOW\1$RESET/g"
like image 182
Adam Rosenfield Avatar answered Oct 08 '22 21:10

Adam Rosenfield