2017-06-01 58 views
-2

enter image description here使用AWK

计算平均分组行驶速度在NS2我需要计算平均分组行驶速度在NS2。我必须在awk程序中编写这个公式。但我不知道该怎么做。 n是接收数据包的数量,s是数据包传输的距离。任何答案都将非常有用。谢谢。

+2

欢迎来到StackOverflow。 “输入图像描述”,即描述链接的数学公式所做的事情。然后编写伪代码来概述算法。然后运用你的awk知识写出你自己的尝试。然后在这里添加你的awk代码来问一个具体的问题。如果您对awk完全陌生,则通过awk教程进行工作可能会有所帮助。您可能需要先参加[导览]。制作[mcve]对于你自己和试图帮助你的人是一件非常有用的事情。 – Yunnosch

+0

向我们展示您的尝试,然后我们可以开始给予支持。 – CWLiu

+0

你的配方不对!总和索引('i')从'n'变为'n'。 't'和's'都不依赖于'i'。 – karakfa

回答

0
function topla(array) {sum=0; for (i in array) {sum=sum+250/array[i];}return sum;} 

BEGIN { 
total_packets_sent = 0; 
total_packets_received = 0; 
total_packets_dropped = 0; 
first_packet_sent = 0; 
last_packet_sent = 0; 
last_packet_received = 0;} 


{event = $1; time = $2; node = $3; type = $4; reason = $5; packetid = $6; 



if (time < simulation_start || simulation_start == 0) 
    simulation_start = time; 
if (time > simulation_end) 
    simulation_end = time; 



if (type == "AGT") { 
    nodes[node] = node; # to count number of nodes 
    if (time < node_start_time[node] || node_start_time[node] == 0) 
     node_start_time[node] = time; 

    if (time > node_end_time[node]) 
     node_end_time[node] = time; 

    if (event == "s") { 
     flows[node] = node; # to count number of flows 
     if (time < first_packet_sent || first_packet_sent == 0) 
      first_packet_sent = time; 
     if (time > last_packet_sent) 
      last_packet_sent = time; 
     # rate 
     packets_sent[node]++; 
     total_packets_sent++; 

     # delay 
     pkt_start_time[packetid] = time; 
    } 
    else if (event == "r") { 
     if (time > last_packet_received) 
      last_packet_received = time; 
     # throughput 
     packets_received[node]++; 
     total_packets_received++; 

     # delay 
     pkt_end_time[packetid] = time; 
    } 
}}` 

END { 



# delay 
for (pkt in pkt_end_time) { 
    end = pkt_end_time[pkt]; 
    start = pkt_start_time[pkt]; 
    delta = end - start; 
    if (delta > 0) { 
     delay[pkt] = delta; 

    } 
} 

result=topla(delay)/total_packets_received; 

printf(result) 

}

我写这篇awk程序。但是它给出了零分的尝试误差。并且我在topla函数中将传输范围写为250,但是我没有得到我想要的结果。我应该如何编写传输范围?