Reputation: 1
when ever I am trying to calculate mean and standard deviation using awk i am getting "awk: fatal: division by zero attempted" error.
my command is
BEGIN{
recvs = 0;
routing_packets = 0;
}
{
state = $1;
layer = $3;
tipe_data = $6;
}
{
if (state == "r" && tipe_data == "tcp" && layer=="AGT") recvs++;
if (state == "s" && layer == "AGT" && tipe_data =="AODV") routing_packets++;
}
END{
printf("##################################################################################\n");
printf("\n");
printf(" Normalized Routing Load = %.3f\n", routing_packets/recvs);
printf("\n");
printf("##################################################################################\n");
}
thanks
Upvotes: 0
Views: 3802
Reputation:
Change this line
printf(" Normalized Routing Load = %.3f\n", routing_packets/recvs);
to
if (recvs)printf(" Normalized Routing Load = %.3f\n", routing_packets/recvs);
Upvotes: 0
Reputation: 4835
If this if check never succeeds the value of recvs will be equal to 0
if (state == "r" && tipe_data == "tcp" && layer=="AGT") recvs++;
Your program will go on nevertheless and hit this line routing_packets/recvs You should handle the scenario where recvs is equal to 0 depending on your requirement.
Upvotes: 0
Reputation: 174696
Yes, values assigned to recvs and routing_packets variables is zero.
recvs = 0;
routing_packets = 0;
And you are trying to to divide it. That is,
routing_packets/recvs
Which results in,
awk: fatal: division by zero attempted" error
Upvotes: 0