#93 Summing 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 “ex93.txt”

#The -F',' tells awk that the field separator for the input is a comma.
#The {sum+=$4;} adds the value of the 4th column to a running total.
#The END{print sum;} tells awk to print the contents of sum after all lines are read.
awk -F ',' '{sum+=$4;} END{print sum;}' test1.txt

Run this script

./ex93.txt

The result is

211.5

Discover more from Tips and Hints for Aerospace Engineers

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

Continue reading