问题描述
问题
所以我有code的以下行:
如果(总和[味精,H] / summsg [味精,H]!= 0)
的printf(%9.2f \\ n,总和[味精,H] / summsg [味精,H])
信息
是一个消息数组包含10个不同的值小时
保存在一个日志文件中的所有小时present。
在总和[]
数组addind一起字段的值(总和[$ 5,$ 3] + = $ 11个
)
和 summsg []
阵列计数的行数( summsg [$ 5,$ 3] ++
)
这是重新调整一个致命:除以零尝试
的错误,但我thorght了 AWK
将评估总和[味精,H] / summsg [味精,H]!= 0
然后再继续。
我也曾尝试检查每个具有以下code中的值:
如果(总和[味精,H]!= 0 || summsg [味精,H]!= 0)
的printf(%9.2f \\ n,总和[味精,H] / summsg [味精,H])
但是,这是我停止计算时间,因为我觉得它是在00-09小时拿起前导0的并返回false。
如果需要,我可以提供完整的code。
任何想法?
注释更新
根据的评论,他们是错别字,改正他们,他们并没有发挥作用。
示例输入文件
MESSAGE1 01 10
消息2 01 01
消息2 01 05
MESSAGE1 01 15
MESSAGE1 01 05
MESSAGE1 02 03
MESSAGE1 02 06
消息2 02 10
消息2 02 20
消息2 02 05
The code to reflect the input file would be as follows:
{
msg_type[$1]++
sum[$1,$2] += $3
summsg[$1,$2]++
}
END {
for (msg in msg_type) {
print msg
for (h = 0; h <= 23; h++) {
if (sum[msg,h] != 0 || summsg[msg,h] != 0)
printf ("%9.2f\n",sum[msg,h]/summsg[msg,h])
}
}
}
For your MCVE code, the problem is that you indexed the arrays with 01
or 02
while loading them, but are trying to extract the data with 1
or 2
(no leading zero). You have to fix that. For example:
{
msg_type[$1]++
sum[$1,$2] += $3
summsg[$1,$2]++
#print "type:", $1, "hr:", $2, "value:", $3
}
END {
for (msg in msg_type) {
print msg
for (i = 0; i <= 23; i++) {
if (i < 10)
h = "0" i
else
h = i
#print " ", msg, h, sum[msg,h], summsg[msg,h]
if (sum[msg,h] != 0 || summsg[msg,h] != 0)
printf("%9.2f\n", sum[msg,h]/summsg[msg,h])
}
}
}
For the sample input, the output becomes:
message1
10.00
4.50
message2
3.00
11.67
I think you should probably print the hour too, but that's your choice.
这篇关于AWK - 做一个if条件计算一个数组输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!