我搜索可以帮助我解决问题的人:
我有一个file.txt,其中包含以下信息:
2014-05-30 15:34:46,170 [18] INFO - Login - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Subscribe - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Subscribe - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - GetInfos - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Subscribe - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Login - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Login - TimeTaken 00:00:00.1866202
副主编:
records=$(grep 'TimeTaken' file.txt)
timems=$(echo "$records" | cut -d " " -f 10 | cut -c 8-)
echo $timems
averageTime=$(echo "$records" | awk '{
print $timems
arr[$6]+=$timems
count[$6]++
}
END {
for (key in arr) printf("%s\,%s\,%s\n", count[key], key, arr[key] / count[key])
}' | sort +0n -1)
echo "$averageTime"
当我启动此脚本时,结果如下:
0.1866202 0.1866202 0.1866202 0.1866202 0.1866202 0.1866202 0.1866202
awk: cmd. line:7: warning: escape sequence `\,' treated as plain `,'
1,GetInfos,2014
3,Login,2014
3,Subscribe,2014
2014-05-30 15:34:46,170 [18] INFO - GetInfos - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Login - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Login - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Login - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Subscribe - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Subscribe - TimeTaken 00:00:00.1866202
2014-05-30 15:34:46,170 [18] INFO - Subscribe - TimeTaken 00:00:00.1866202
对于1的最后一个字段,GetInfos,2014(即:2014),我希望通过我的方法获得timeTaken组的平均值。 我不明白为什么当我制作" echo $ timems"我在" averageTime"之外有很好的价值。变量不是里面好的。
希望有人可以帮助我。
非常感谢!
答案 0 :(得分:1)
在您的脚本$timems
中存储值列表。这就是为什么你不能用awk中的这个变量来计算的。
以下代码可能符合您的需求。
averageTime=$(grep 'TimeTaken' file.txt | awk -F'[ :]' '{
arr[$9]+=$14
count[$9]++
}
END {
for (key in arr) printf("%s,%s,%.7f\n", count[key], key, arr[key] / count[key])
}' | sort +0n -1)
echo "$averageTime"