woff
woff

Reputation: 107

Split text from bash variable

I have a variable which has groups of numbers. It looks like this:

foo 3
foo 5
foo 2
bar 8
bar 8
baz 2
qux 3
qux 5
...

I would like to split this data so I can work on one 'group' at a time. I feel this would be achievable with a loop somehow. The end goal is to take the mean of each group, such that I could have:

foo 3.33
bar 8.50
baz 5.00
qux 4.00
...

This mean taking has been implemented already, but I've brought it up so the context is known.

It's important to note that each group (eg. foo, bar, baz) is of arbitrary length.

How would I go about splitting up these groups?

Upvotes: 1

Views: 50

Answers (1)

Ben
Ben

Reputation: 6348

I would use awk (tested using the GNU version gawk here, but I think it's portable) for both the collecting and the averaging. As a coreutil, it should be in just about anything bash is installed on.

# print_avg.awk
{
    sums[$1] += $2
    counts[$1] += 1
}
END {
    for (key in sums)
        print key , sums[key] / counts[key]
}

data.txt:

foo 3
foo 5
bar 8
bar 8
baz 2
qux 3
qux 5

Run it like:

$ awk -f print_avg.awk data.txt 
foo 4
baz 2
qux 4
bar 8

Upvotes: 2

Related Questions