awk中,计算平均的时间间隔不同 [英] awk, calculate the average for different interval of time

查看:150
本文介绍了awk中,计算平均的时间间隔不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人可以教我如何计算平均值的时间差之间?例如:

can anybody teach me how to calculate the average for between the difference of time? for example

    412.00 560.00 
    0 0 
    361.00 455.00 561.00 
    0 0 
    0 0 
    0 0 
    237.00 581.00 
    425.00 464.00 
    426.00 520.00 
    0 0 

正常情况下,它们由总组数的做所有这些号码分的总和

the normal case, they do the sum of all of those number divide by total set of number

    sum/NR

这里的挑战


  1. 列的数目是动态的,这意味着并不是所有的行具有相同数量的列

  2. 计算平均值,例如我们有这样的:361.00 455.00 561.00

  1. the number of column is dynamic, which mean not all of the line have the same number column
  2. to calculate the average , example we have this : 361.00 455.00 561.00

    so the calculation :
    ((455-361) + (561 - 455))/2


所以,我期待的输出是这样的:

so, the output i'm expecting is like this :

      total_time divided_by average
      148        1          148
      0          1          0
      200        2          100
      0          1          0
      0          1          0
      0          1          0
      344        1          344
      :          :          :
      :          :          :
      :          :          : 

我尝试用awk,但我坚持...

im trying to use awk, but i stuck...

推荐答案

上具有三个或更多的时间值的行的中间值是无意义的 - 只的值的事项的数目。要查看该从你的例子中,注意:

The intermediate values on lines with three or more time values are meaningless -- only the number of values matters. To see this from your example, note that:

((455-361) + (561 - 455))/2 = (561 - 361) / 2

因此​​,你真的只需要做的是这样

Thus, you really just need to do something like

cat time_data |
  awk '{ printf("%f\t%d\t%f\n", ($NF - $1), (NF - 1), ($NF - $1) / (NF - 1)) }'

有关您的样本数据,这给你指定的结果(虽然不是很好,你present它格式化)。

For your sample data, this gives the results you specify (although not formatted as nicely as you present it).

此假定时间值在线路排序。如果不是这样,计算出最大值和最小值,并更换 $ NF $ 1 使用,分别为。

This assumes that the time values are sorted on the lines. If not, calculate the maximum and minimum values and replace the $NF and $1 uses, respectively.

这篇关于awk中,计算平均的时间间隔不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆