awk, calculate the average for different interval of time

戏子无情 提交于 2020-01-04 06:24:13

问题


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

the challenge here

  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
      :          :          :
      :          :          :
      :          :          : 

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


回答1:


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)) }'

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

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.




回答2:


A bash script:

#!/bin/bash
(echo "total_time divided_by average"
while read line
do
 arr=($line)
 count=$((${#arr[@]}-1)) 
 total=$(bc<<<${arr[$count]}-${arr[0]})
 echo "$total $count $(bc<<<$total/$count)"
done < f.txt ) | column -t

Output

total_time  divided_by  average
148.00      1           148
0           1           0
200.00      2           100
0           1           0
0           1           0
0           1           0
344.00      1           344
39.00       1           39
94.00       1           94


来源:https://stackoverflow.com/questions/6198882/awk-calculate-the-average-for-different-interval-of-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!