[net.sources] An example awk script

david@ukma.UUCP (David Herron, NPR Lover) (03/12/85)

# avg.awk - Average the columns of a table of numbers.  
# The table may have varying numbers of fields, any number of fields,
# or any number of records.  Missing fields are assumed to be 0.
#
# Usage:
#	awk -f avg.awk <table >out
#
# It prints a single line having one number per field, which is the
# average for that field over the whole table.
#
# Author:
#	David Herron	(ukma!david)
#	University of Kentucky
#

BEGIN	{
		maxnf = 0;
	}
	{
		for (i=1; i<=NF; i++) {
			tab[i] = tab[i] + $(i);
		}
		if (NF > maxnf) 
			maxnf = NF;
	}
END	{ 
		for (i=1; i<=maxnf; i++)
			printf " %d ",(tab[i]/NR);
		printf "\n";
	}