i have file looks this:
aaa 15 aaa 12 bbb 131 bbb 12 ccc 123 ddddd 1 ddddd 2 ddddd 3
i sum each unique element in left side , calculate how many of each element summed up:
aaa 27 - 2 bbb 143 - 2 ccc 123 - 1 ddddd 6 - 3
how accomplish awk or similar?
you can in awk
collecting sums 2 arrays, using column 1 key both arrays (then pipe sort
if desired):
awk '{sums[$1] += $2; counts[$1] += 1} end {for (key in sums) {print key, sums[key], "-", counts[key]}}' file | sort
output:
aaa 27 - 2 bbb 143 - 2 ccc 123 - 1 ddddd 6 - 3
Comments
Post a Comment