abh
abh

Reputation: 101

Add 4th column when first 3 columns are identical

I have file like this in which some of the lines are identical in first 3 columns and different values in 4th column.i want to sum all the values in the 4th column if first 3 columns are identical:

chr1 241783  286397   52
chr1 241783  286397   53
chr1 241783  286397   53
chr2 10500  25700   57
chr2 10500  25700   59
chr2 10500  25700   59

I want the output file like this:

chr1 241783  286397 158
chr2 10500  25700   175

I can print out the single identical value from the file using awk which I saw in one of the post by awk '!array[$1,$2,$3]++' .but I want to sum of them.

Upvotes: 0

Views: 69

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85785

This should do the trick:

$ awk '{a[$1FS$2FS$3]+=$4}END{for(k in a)print k,a[k]}' file
chr2 10500 25700 175
chr1 241783 286397 158

Upvotes: 2

Related Questions