Thursday, 30 August 2018

Sum of column values ​​using the awk command

I want to sum the values of all rows in the column 3. How can I do this?

Input:
chr19   10 11
chr19   12 15
chr19   11 29
chr19   a0 20

Expected output:
75

Just store the value of third column into a variable and add that value with the value present in the third column of next line, likewise for all. Atlast, the variable count contains the sum of all the numbers present in the third column.
$ awk '{count=count+$NF}END{print count}' file
75
$ awk '{count=count+$3}END{print count}' file
75

0 comments:

Post a Comment