#93 Summing values of a column using awk command
Assume we have “test1.txt” file with data in columns:
1 | a,a,aa, 1 |
2 | a,a,aa, 2 |
3 | d,d,dd, 7 |
4 | d,d,dd, 9 |
5 | d,dd,d, 0 |
6 | d,d,dd, 23 |
7 | d,d,dd, 152 |
8 | d,d,dd, 7 |
9 | d,d,dd, 5 |
10 | f2,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. |
4 | awk - F ',' '{sum+=$4;} END{print sum;}' test1.txt |
Run this script
1 | . / ex93.txt |
The result is
1 | 211.5 |
Recent Comments