Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site ukma.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!ihnp4!cbosgd!ukma!david
From: david@ukma.UUCP (David Herron, NPR Lover)
Newsgroups: net.sources
Subject: An example awk script
Message-ID: <1263@ukma.UUCP>
Date: Mon, 11-Mar-85 16:17:29 EST
Article-I.D.: ukma.1263
Posted: Mon Mar 11 16:17:29 1985
Date-Received: Tue, 12-Mar-85 23:05:56 EST
Organization: Univ. of KY Mathematical Sciences
Lines: 30

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