egorulz
egorulz

Reputation: 1505

Consolidating columns based on field value in bash

23:21,74.285714,199924
23:21,80.000000,627377
23:21,82.857143,499796
23:22,85.714286,670676
23:22,87.428571,149860
23:22,88.000000,301272

The input data set I'm working with looks like this.

What I'm trying to do is to consolidate data on a per minute (1st field) basis. The 2nd column values should be averaged and the 3rd field values should be summed.

So my output should look like:

23:21,78,127377
23:22,87,105678

The values above are sample, not really summed or averaged, but that's the gist. I'm trying to do this with awk right now, is there a better option?

Upvotes: 2

Views: 67

Answers (1)

anubhava
anubhava

Reputation: 786001

You can try awk:

awk -F, '{a[$1]+=$2; b[$1]+=$3; c[$1]++}
          END {for (i in a) print i, int(a[i]/c[i]), b[i]}' OFS=, file
23:21,79,1327097
23:22,87,1121808

Upvotes: 2

Related Questions