问题
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 values
hr
holds all hours present in a log file.
the sum[]
array is addind together values of a field (sum[$5,$3] += $11
)
and the summsg[]
array is counting the number of lines (summsg[$5,$3]++
)
This is retuning a fatal: division by zero attempted
error, but I thorght that awk
would evaluate the sum[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])
}
}
}
回答1:
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.
来源:https://stackoverflow.com/questions/38023336/awk-does-an-if-condition-calculate-an-array-input