An example awk script

David Herron, NPR Lover david at ukma.UUCP
Tue Mar 12 07:17:29 AEST 1985


# 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";
	}



More information about the Comp.sources.unix mailing list