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
- the number of column is dynamic, which mean not all of the line have the same number column
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...
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.
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
精彩评论