Add up a column with the same key feature in csv file

239 views Asked by At

The columns including two key features, one column to sum up, and some other (e.g,1) column that is not important.

key1, key 2, pr, trivial    
abc, 43, 23, haha    
abc, 43, 456, hok    
bcd, 23, 89,kol

I want to add the sum column with the SAME key1 and key2, and output a csv file with 3 columns.

key1, key2, sumvalueofpr

in the above case, it is

key1, key2, sumvalueofpr
abc , 43, 479
bdc, 23, 89

(note: 479=23+456)

To do with either Perl or Shell command is ok.

2

There are 2 answers

0
JonathanV On

Is there only a particular value of key1/key2 pairs or do you want to check for any possible matches?

You could do a lower performance (but easy to code) method you could try a method like this:

for i in range(0,number_of_rows):
   for j in range(i+1, number_of_rows):
        if (key1_from_row_i == key1_from_row_j) and (if (key1_from_row_i == key1_from_row_j)):
             increment your sum of key1_sum key2_sum
0
Kent On

if awk is accepted by you, this one-liner gives you expected output:

awk -F, -v OFS=", " 'NR==1{print "key1","key2","sum";next}{a[$1", "$2]+=$3}END{for(x in a)print x,a[x]}' file