[ILUG] perl file processing

Brian Foster blf at utvinternet.ie
Wed Oct 8 20:05:20 IST 2008


 below's a quickly-put-together all-awk(1) solution,
 albeit if this was my problem I'd be more inclined
 to do some filtering first, probably with sed(1)
 like Francis did.
cheers!
	-blf-

#!/bin/gawk -f
BEGIN {
	state  = 0
	ncols  = 0
	nlines = 0

	STDERR = "/dev/stderr"
}

state == 0 && $0 == "=== Stratified cross-validation ===" {
	state = 1
	next
}

state == 1 && $0 == "=== Detailed Accuracy By Class ===" {
	state = 2
	next
}

state == 2 && 2 <= NF && $NF ~ /^[A-Z]$/ {
	for (n = 1; n < NF; n++) {
		if ($n !~ /^[0-9.]*$/)
			next
	}
	state = 3
	ncols = NF
}

state == 3 && NF != ncols { exit }	# goto END

state == 3 && $NF !~ /^[A-Z]$/ { exit }	# goto END

state == 3 {
	for (n = 1; n < ncols; n++)
		col[n] += (0 + $n)
	nlines++
	next
}

END {
#debug	print "EXIT(" FNR "): nlines=" nlines, "ncols=" ncols
	if (nlines <= 0) {
		print FILENAME ": Data not found, state =", state  >STDERR
		exit 1
	}
	for (n = 1; n < ncols; n++)
		print col[n]/nlines
}

-- 
“How many surrealists does it take to    |  Brian Foster
 change a lightbulb?  Three.  One calms  |  somewhere in south of France
 the warthog, and two fill the bathtub   |     Stop E$$o (ExxonMobil)!
 with brightly-coloured machine tools.”  |       http://www.stopesso.com



More information about the ILUG mailing list