Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts

Thursday, May 1, 2008

Colorize your log files!

I posted my awk colorizer for tail before and the general consensus seemed to be "meh".

If you are greping through monochrome logs, you're missing out on color.

Here's my latest:

grep -l search criteria * | xargs head | awk -f colorit.awk | more

great for spam logs.

Wednesday, April 2, 2008

awk before and after

This is attempt number one for my friend who wanted awk based before-and-after.

It's not very interactive, and needs some command line features. The point, I guess, is that you could use this in conjunction with my colorize awk function to have context sensitive searching of words in a way that less can be useful. ETA: Oh, yeah, the point was to add "before" context to tail-f filename | grep "search" , similar to how grep does it on static files. In this case, it's more of tail -f filename | awk -f thisfile.awk, and you could also pipe that through my awk colorizer for the double whammy: context (before and after search) plus color.


BEGIN {
numlinesbefore = 3
numlinesafter = 3
needle = "search"
aftercount = -1
head = ""
tail = ""
}
{
before[numlinesbefore] = $0
for (i = 0; i < numlinesbefore; i++) {

before[i] = before[i+1]
}

if ($0 ~ needle) {
for (i = 0; i < numlinesbefore-1; i++) {
head = head "\n" before[i]
}
aftercount = numlinesafter + 1
tail = ""
print head
}
if (aftercount >= 0) {
print $0
--aftercount
}

if (aftercount == 0 ) {
head = ""
tail = ""
print "---"
aftercount = -1
}
}


Of course,this is an awk program.

awk colorizer for tail.

One of the problems I have with tail -f maillog.txt | grep "search" is that I really did want to watch the noise and not just the signal. Except that I'd like to notice the signal. Here's my printcolor.awk Now I can use tail -f maillog.txt | awk -f printcolor.awk and see the whole tail, with keywords highlighted in *different* colors.

NOTE: ^[ is supposed to be Ctrl-v, Escape; NOT caret, left bracket.


function colorize(word, color)
{
c["red"] = "^[[1;31;40m"
c["green"] = "^[[1;32;40m"
c["yellow"] = "^[[1;33;40m"
c["blue"] = "^[[1;34;40m"
c["magenta"] = "^[[1;35;40m"
if (line ~ word)
{ split (line, a, word)
line=a[1] c[color] (word) "^[[0;37;40m" a[2]
}
}
{line = $0
colorize("whitelist","green")
colorize("Bayesian Spam","red")
print line
}



ETA: If you'd like to be annoyed/beeped at for something that you're looking for, you can add ctrl-v, ctrl-g in the right hand side, eg: "^G^[[1;32;40m" (not caret G)

Blog Archive