Count time min/max/average for ping

I am redirecting my ping output to a file. The sample output is like this:

64 bytes from xx.xx.xx.167: icmp_seq=4490 ttl=116 3.75 ms 2011Jul12- 15 40 16
64 bytes from xx.xx.xx.167: icmp_seq=4491 ttl=116 5.29 ms 2011Jul12- 15 40 17
64 bytes from xx.xx.xx.167: icmp_seq=4492 ttl=116 4.88 ms 2011Jul12- 15 40 18
64 bytes from xx.xx.xx.167: icmp_seq=4493 ttl=116 4.33 ms 2011Jul12- 15 40 19
64 bytes from xx.xx.xx.167: icmp_seq=4494 ttl=116 3.93 ms 2011Jul12- 15 40 20
64 bytes from xx.xx.xx.167: icmp_seq=4495 ttl=116 4.05 ms 2011Jul12- 15 40 21
64 bytes from xx.xx.xx.167: icmp_seq=4496 ttl=116 3.33 ms 2011Jul12- 15 40 22
64 bytes from xx.xx.xx.167: icmp_seq=4497 ttl=116 4.23 ms 2011Jul12- 15 40 23
64 bytes from xx.xx.xx.167: icmp_seq=4498 ttl=116 3.59 ms 2011Jul12- 15 40 24
64 bytes from xx.xx.xx.167: icmp_seq=4499 ttl=116 3.57 ms 2011Jul12- 15 40 25
64 bytes from xx.xx.xx.167: icmp_seq=4500 ttl=116 5.27 ms 2011Jul12- 15 40 26
64 bytes from xx.xx.xx.167: icmp_seq=4501 ttl=116 4.51 ms 2011Jul12- 15 40 27
64 bytes from xx.xx.xx.167: icmp_seq=4502 ttl=116 3.45 ms 2011Jul12- 15 40 28
64 bytes from xx.xx.xx.167: icmp_seq=4503 ttl=116 4.16 ms 2011Jul12- 15 40 29
64 bytes from xx.xx.xx.167: icmp_seq=4504 ttl=116 3.34 ms 2011Jul12- 15 40 30
64 bytes from xx.xx.xx.167: icmp_seq=4505 ttl=116 4.59 ms 2011Jul12- 15 40 31
64 bytes from xx.xx.xx.167: icmp_seq=4507 ttl=116 3.46 ms 2011Jul12- 15 40 33
64 bytes from xx.xx.xx.167: icmp_seq=4508 ttl=116 5.34 ms 2011Jul12- 15 40 34

The reply time (rtt) is the seventh field (e.g. 5.34 for the last line).
How can I use awk to produce minimum, maximum and average for the rtt from all lines in the file? How to do it without awk?

awk 'NR==1{min=$7;max=$7;sum+=$7;next}{if ($7<min) min=$7;if ($7>max) max=$7;sum+=$7}END{print "min: "min", max: "max", avg: "sum/NR}' file
1 Like
 
bash-3.00$ ./test.ksh
3.33
5.34
4.17056
bash-3.00$ cat test.ksh
nawk '{print $7}' test.txt | sort -n  | head -1       #min
nawk '{print $7}' test.txt | sort -nr  | head -1      #max
nawk '{a=a+$7} END{print a/NR}' test.txt            #avg

1 Like

How can I make it so that the whole line will also be displayed, not just the min, max or avg?

min=`nawk '{print $7}' test.txt | sort -n  | head -1`       #min
max=`nawk '{print $7}' test.txt | sort -nr  | head -1`      #max
avg=`nawk '{a=a+$7} END{print a/NR}' test.txt`            #avg
echo -n "Minimum : " 
grep $min test.txt
echo -n "Maximum : " 
grep $max test.txt
awk 'NR==1{min=$7;min_l=$0;max_l=$0;max=$7;sum+=$7;next}{if ($7<min) {min=$7;min_l=$0};if ($7>max) {max=$7;max_l=$0};sum+=$7}END{print "min: "min_l"\nmax: "max_l"\navg: "sum/NR}' file
awk 'BEGIN{print "min\tmax\taverage"}{if(NR==1){min=max=average=$7}else{min=min>$7?$7:min;max=max<$7?$7:max;average+=$7}}END{print min"\t"max"\t"average/NR}' inputfile