#94 Average of values of a column using awk command

Assume we have “test1.txt” file with data in columns:

a,a,aa,1
a,a,aa,2
d,d,dd,7
d,d,dd,9
d,dd,d,0
d,d,dd,23
d,d,dd,152
d,d,dd,7
d,d,dd,5
f2,f2,f2,5.5

Save the following awk script in the file “ex94.awk”

#!/bin/awk -f
{ sum += $4 }
END { if (NR > 0) 
print "Number of rows: " NR
print "Sum: " sum
print "Average: " sum/NR
}

Run file “ex94run.txt”

awk -f ex94.awk test1.txt

using

./ex94run.txt

The result is

Number of rows: 10
Sum: 211.5
Average: 21.15

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading