0

我有一个示例文件,例如

stat1:  sample:        0.0
stat2:  sample:        0.0
stat3:  sample:        349.7

stat1:  sample:        0.0
stat2:  sample:        0.0
stat3:  sample:        349.7

stat1:  sample:        0.0
stat2:  sample:        0.0
stat3:  sample:        349.7

我要做的是找到每个统计数据的最大值和平均值。

4

4 回答 4

1

如果您不介意使用 Perl:

perl -lane '{ if(/(stat\d+)/) {
    $m{$1} =$F[2] if $m{$1}<$F[2] || !$m{$_};
    $s{$1}+=$F[2]; $c{$1}++
}} END{print "$_: $m{$_},".$s{$_}/$c{$_} for keys %c}' input
于 2013-04-16T19:17:47.873 回答
1
awk '{
         if (NF != 3) next
         sum[$1] += $3;
         if (cnt[$1]++ == 0) { max[$1] = $3; min[$1] = $3; }
         if ($3 > max[$1]) max[$1] = $3
         if ($3 < min[$1]) min[$1] = $3
     }
     END {
         printf "%-8s %4s %8s %8s %8s\n", "Sample", "N", "Minimum", "Maximum", "Average"
         for (key in sum)
         {
             printf "%-8s %4d %8.2f %8.2f %8.2f\n", key, cnt[key], min[key], max[key], sum[key]/cnt[key]
         }
     }' data-file

示例输出(来自问题中的数据,这非常令人兴奋):

Sample      N  Minimum  Maximum  Average
stat2:      3     0.00     0.00     0.00
stat1:      3     0.00     0.00     0.00
stat3:      3   349.70   349.70   349.70

此代码包括最小值和最大值;如果不需要,很容易删除。请注意,它会跳过数据文件中的空白行。

于 2013-04-16T19:28:37.943 回答
1

使用 awk 关联数组非常简单:

平均最大值.awk

# Skip empty lines
NF==0 { next }

# Keep a tally of number of elements and their sum
{ cnt[$1]++; sum[$1] += $3 }

# If max[$1] has not been defined or if it is smaller than $3
cnt[$1] == 1 || max[$1] < $3 {
  max[$1] = $3
}

END {
  for (k in sum)
    print k, max[k], sum[k]/cnt[k]
}

像这样运行它:

awk -f meanmax.awk infile

输出:

stat1: 0.0 0                    
stat2: 0.0 0
stat3: 349.7 349.7

或者,如果在最初发布的输入上运行:

stat1: 18.0 10.3333             
stat2: 45.0 16.6667
stat3: 239.7 89.4667
于 2013-04-16T19:30:19.650 回答
1

如果您需要所有值的平均值和最大值,您可以尝试这样的事情

awk '/sample:/ {s[$1] += $3; if(++n[$1]==1 || max[$1]<$3) max[$1] = $3}
END { for (i in s) print i" mean = "s[i]/n[i]", max = "max[i] }
' <<EOT
stat1:  sample:        0.0
stat2:  sample:        0.0
stat3:  sample:        349.7

stat1:  sample:        0.0
stat2:  sample:        0.0
stat3:  sample:        349.7

stat1:  sample:        0.0
stat2:  sample:        0.0
stat3:  sample:        349.7
EOT

输出:

stat3: mean = 349.7, max = 349.7
stat1: mean = 0, max = 0.0
stat2: mean = 0, max = 0.0
于 2013-04-16T19:34:16.900 回答