awk calculation

Hallo all,

I have a script which creates an output ... see below:

root@a7germ:/tmp/pax [136] > cat 20061117.txt
523.047
521.273
521.034
517.367
516.553
517.793
513.114
513.940

I would like to use awk to calculate the (a)total sum of the numbers (b) The average of the numbers.

Please help me and regards,

Ok guys it doesnt have to awk......is there another way i can do the calculations?

awk 'BEGIN {sum=0;avg=0;}{sum+=$1} END {avg=sum/NR;print sum;print avg}' <data

Assumes no white space/extra lines in the data file. :wink:

John Arackal

awk '{s+=$1}END {printf("sum=%f Average=%f\n",s,s/NR)}' file

Python alternative:

#!/usr/bin/python
data = open("20061117.txt").read().split()
total = sum([ float(i) for i in data ])
print "Sum is " , total
print "Average is "  , total/len(data)