致命:尝试除零

时间:2014-07-02 06:56:23

标签: awk

当我试图使用awk计算平均值和标准偏差时我得到了" awk:致命:除零尝试"错误。

我的命令是

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");
} 

感谢

3 个答案:

答案 0 :(得分:0)

是,分配给recvs和routing_packets变量的值为零。

recvs = 0;
routing_packets = 0;

你正试图分裂它。也就是说,

routing_packets/recvs

结果是,

awk: fatal: division by zero attempted" error

答案 1 :(得分:0)

如果这次检查永远不会成功,则recvs的值将等于0

if (state == "r" && tipe_data == "tcp" && layer=="AGT") recvs++;

您的程序仍将继续并点击 routing_packets / recvs 这一行 您应该根据您的要求处理recvs等于0的场景。

答案 2 :(得分:0)

更改此行

printf("                       Normalized Routing Load = %.3f\n", routing_packets/recvs);

if (recvs)printf("                       Normalized Routing Load = %.3f\n", routing_packets/recvs);