Using awk with group by functionality

You can use the following example if you need to total numbers over a group in a given file.

We first show our sample file…

14:30:57 oracle@emgrid01 ~ >cat list.txt
steve:61
steve:14
becky:57
steve:19
jenna:69
stephen:57
maddie:54
jenna:53
abby:41
jenna:21
jenna:66
jenna:64
stephen:53
stephen:26
jenna:77
steve:46
maddie:39
steve:32
abby:77
jenna:97

…and then show the totals using awk…

14:30:59 oracle@emgrid01 ~ >awk -F ":" '{a[$1]+=$2} END {for (i in a) {print i,a[i]}}' list.txt
maddie 93
abby 118
jenna 447
becky 57
steve 172
stephen 136
14:31:01 oracle@emgrid01 ~ >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.