Ryan Zapotocny
Ryan Zapotocny

Reputation: 53

Total Values of Rows having same value in specific column

I want to total the value in the 3rd and 4th columns of rows having the same value in column 2.

My file looks something like the following:

18 1600012 0 8
18 1600014 0 3
18 1600014 0 4
18 1600014 0 5
18 1600015 0 5
18 1600015 4 4

and the desired output would be

18 1600012 0 8
18 1600014 0 12
18 1600015 4 9

Upvotes: 1

Views: 57

Answers (1)

anubhava
anubhava

Reputation: 785286

You can use awk like this:

awk '{k=$1 OFS $2; c3[k] += $3; c4[k] += $4} END{for (i in c3) print i, c3[i], c4[i]}' file

Output:

18 1600014 0 12
18 1600015 4 9
18 1600012 0 8

Upvotes: 1

Related Questions