autodidacte
autodidacte

Reputation: 43

Addition if identical value over line

I have a CSV like:

1015,5
1015,4
1035,17
1035,11
1009,1
1009,4
1026,9
1004,5
1004,5
1009,1

I search a way to obtain : an addition of the second number if the first number match

1015,9
1035,28
1009,6
1026,9
1004,10

Upvotes: 2

Views: 41

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185434

Try this :

awk 'BEGIN{FS=OFS=","}{a[$1]+=$2}END{for(i in a){print i,a[i]}}' file

This is the snippet that every shell coder should know from the top of his head.

Upvotes: 4

Related Questions