Using awk with group by functionality

Thursday, August 12, 2010
By Steve

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…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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…

1
2
3
4
5
6
7
8
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