AWK - 做一个if条件计算一个数组输入 [英] AWK - does an if condition calculate an array input
问题描述
问题
所以我有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
这是一个由输入文件
块引用>在code,以反映输入文件将如下所示:
{
MSG_TYPE [$ 1] ++
综上所述[$ 1,$ 2] + = $ 3
summsg [$ 1,$ 2] ++
}
结束 {
对(在MSG_TYPE MSG){
打印味精
对于(H = 0; H&LT = 23; H ++){
如果(总和[味精,H]!= 0 || summsg [味精,H]!= 0)
的printf(%9.2f \\ n,总和[味精,H] / summsg [味精,H])
}
}
}
解决方案有关您的MCVE code,问题是你索引的阵列,
01
或02 加载它们,但试图提取与
1
或2 $的数据code> C $ C>(没有前导零)。你必须解决这个问题。例如:
{
MSG_TYPE [$ 1] ++
综上所述[$ 1,$ 2] + = $ 3
summsg [$ 1,$ 2] ++
#PRINT类型:$ 1,HR,$ 2,价值,$ 90
}
结束 {
对(在MSG_TYPE MSG){
打印味精
对于(i = 0; I< = 23;我++){
如果(ⅰ小于10)
H =0我
其他
H =我
#PRINT,味精,H,总和[味精,H],summsg [味精,H]
如果(总和[味精,H]!= 0 || summsg [味精,H]!= 0)
的printf(%9.2f \\ n,总和[味精,H] / summsg [味精,H])
}
}
}有关样品输入,输出变为:
MESSAGE1
10.00
4.50
MESSAGE2
3.00
11.67我想你也许应该打印小时了,但是这是你的选择。
Question
so I have the following line of code:
if (sum[msg,h]/summsg[msg,h] != 0) printf ("%9.2f\n",sum[msg,h]/summsg[msg,h])
msg
is a message array holds 10 distinct valueshr
holds all hours present in a log file. thesum[]
array is addind together values of a field (sum[$5,$3] += $11
) and thesummsg[]
array is counting the number of lines (summsg[$5,$3]++
)This is retuning a
fatal: division by zero attempted
error, but I thorght thatawk
would evaluate thesum[msg,h]/summsg[msg,h] != 0
and then continue.I have also tried checking for each of the values with the following code:
if (sum[msg,h] != 0 || summsg[msg,h] != 0) printf ("%9.2f\n",sum[msg,h]/summsg[msg,h])
But this is stopping my hour calculation as I think it is picking up the leading 0 's on hours 00-09 and returning false.
I can provide the full code if needed.
Any ideas?
Comment Update
As per the comments, they were typos, corrected them and they didn't make a difference.
Sample input file
message1 01 10 message2 01 01 message2 01 05 message1 01 15 message1 01 05 message1 02 03 message1 02 06 message2 02 10 message2 02 20 message2 02 05
This is a made up input file
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
or02
while loading them, but are trying to extract the data with1
or2
(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条件计算一个数组输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!