开发者

how do i process this file using awk or perl?

I want to process some log file whose format is like:

<itemA>    <num>
<itemB>    <num>
<itemC开发者_如何学C>    <num>

I want to get a summary that contains the following information

for each item I want to know the range of the <num> field and the average of <num>


use Statistics::Lite;

while (<>)
{
   my ($a,$b) = split;
   push(@{$items{$a}},$b);
}
foreach my $key (keys %items)
{
  print "$key\n";
  print Statistics::Lite::statsinfo(@{$items{$key}});
}

Obviously you could do the statistics manually, but…why bother?


I agree with Seth, and prefer his answer. Here is a manual solution in Awk, for learning purposes:

#!/usr/bin/awk -f
#invoke with: < infile stats.awk
{
  i = $1; v = $2;
  count[i]++; 
  sum[i] += v; 
  if (v > max[i] || count[i] == 1) { max[i] = v }; 
  if (v < min[i] || count[i] == 1) { min[i] = v };
} 

END {
  for (i in count) {
    print i " range: " min[i] ".." max[i] " avg: " sum[i] / count[i] 
  }; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜