#93 Summing values of a column using awk command

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

1a,a,aa,1
2a,a,aa,2
3d,d,dd,7
4d,d,dd,9
5d,dd,d,0
6d,d,dd,23
7d,d,dd,152
8d,d,dd,7
9d,d,dd,5
10f2,f2,f2,5.5

Save the following awk script in the file “ex93.txt”

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

Run this script

The result is

Discover more from Tips and Hints for Aerospace Engineers

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

Continue reading