Reputation: 101
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
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