Awk Associative Arrays Rule!

published on 2021-11-10 in computing

Awk associative arrays rule!

file.dat:

foo 234
bar 43
baz 109
bar 823
foo 283
baz 23

awk '{a[$1] += $2} END {for (i in a) print i,a[i]}' file.dat

output:

baz 132
foo 517
bar 866

Now, do this on a file with 100,000 lines.

Tags: unix