Pete's Linux Advent Calendar 2007

The 19th day

Sum and average

Here is a quick way to get the sum and average of numbers read from stdin:
#!/usr/bin/awk -f
{
    sum += $1
}
END {
    print "sum:", sum
    print "average: ", sum/NR
}
and here how to extract a given column of a file read from stdin:
#!/usr/bin/awk -f
BEGIN {
	col=ARGV[1]
	delete ARGV[1]
}
{
	print $(col)
}

Pete's Linux Advent Calendar 2007

Valid HTML 4.01!

Comments? send mail to pkruse@arcor.de my monogram