AWK Text Processing – Sum Values in 5th Column by 2nd Column Field

awktext processing

Considering below file:

0,2,,,10
0,2,,,15
0,1,,,984
0,2,,,9
1,14,,,5

Using awk, I need to calculate the total value in $5 per each $2.

The desired output would look like below:

2,34
1,984
14,5

Best Answer

Try:

awk -F, '{a[$2]+=$5};END{for(i in a)print i","a[i]}' <file

A note that array traversal in POSIX awk is unspecified order.

Related Question