gpt4 book ai didi

linux - CSV 文件 bash 脚本中的平均值

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:06 28 4
gpt4 key购买 nike

我目前正在编写一个 bash 脚本来找出服务器每小时的平均内存使用情况,并将其输出到 .csv 文件。将会发生的情况是,脚本将每 10 分钟运行一次,在一小时内运行六次后,我的 .csv 文件中将有 6 个不同的小时值,等等。

我想做的是使用脚本找出每小时的平均值。

#date(YYYYMMDDHHmm) total     used
201811270000 10 3
201811270010 10 4
201811270020 10 5
201811270030 10 9
201811270040 10 8
201811270050 10 2
201811270100 10 5
201811270110 10 1
201811270120 10 7
201811270130 10 6
201811270140 10 5
201811270150 10 2
201811270200 10 1

根据上面的输出,有谁知道我可以找到每小时平均值的方法吗?例如:

The average of hour 201811270000: 5.166666666666667
The average of hour 201811270100: 4.333333333333333

我该如何解决这个问题?

可以这样做吗?

最佳答案

尴尬,

awk '
function calc() {
if (count) print "The average of hour " date ": " (sum/count);
count=0; sum=0; date=$1;
}
/^#/ {next} # throw away comment lines
$1~/00$/ {calc()} # full hour, time to calculate/reset variables
END {calc()} # end of file, ditto
{count+=1; sum+=$3;} # update variables at each line
' < file.txt

纯 bash 将非常困难,因为您需要首先实现浮点算术库。 :)

关于linux - CSV 文件 bash 脚本中的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53517058/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com