Sum of the Values of Column Using AWK.

For example, I have a file called apples.txt with the following content and I want to total them up.

Apples in Box 1 = 100
Apples in Box 2 = 564
Apples in Box 3 = 214
Apples in Box 4 = 124
Apples in Box 5 = 785
Apples in Box 6 = 398
Apples in Box 7 = 125
Apples in Box 8 = 753
Apples in Box 9 = 357
Apples in Box 10 = 951

So just cat the file and use AWK command awk ‘{SUM+=$6}END{print SUM}’ to total them up

make sure to use the right column in awk command, like in our example I want to SUM the column 6 so I used ‘{SUM+=$6}

[root@server ~]# cat  example.txt | awk '{SUM+=$6}END{print SUM}'
4371

Leave a Reply

Your email address will not be published. Required fields are marked *