user3133271
user3133271

Reputation: 25

awk to find repeated occurrence and sum specific column

what's the way to use awk to find repeated occurrence of string from a specific column & sum the adjacent column values? My input:

A B C_1 10
A B D_0 2
A B C_1 2
A B D_2 15
A B D_0 3

output should be
2 C_1 12
2 D_0 5
1 D_2 15

Thanks much.

Upvotes: 0

Views: 436

Answers (1)

Håkon Hægland
Håkon Hægland

Reputation: 40758

You can try the following code:

awk '
{
    a[$3]++
    b[$3]+=$4
}

END {
    for (i in a)
        print a[i], i, b[i]
}' file

with output:

2 D_0 5
2 C_1 12
1 D_2 15

Upvotes: 4

Related Questions