使用Linux命令的awk工具
delay_avg.sh脚本内容如下所示:
#!/bin/sh RESPONSEFILE=$1 if [ $# -ne 1 ] then echo "usage: ./delay_avg.sh named.response.X " else { cat $RESPONSEFILE |awk -F '|' '{print$6}' >>1.txt cat 1.txt |awk '{sum+=$1}END{print "Average = ", sum/NR}' rm -rf 1.txt } fi
执行结果如下:
拓展一下:求最大值或者最小值
比如求最大值: cat data|awk 'BEGIN {max = 0} {if ($1>max) max=$1 fi} END {print "Max=", max}'
求最小值(min的初始值设置一个超大数即可):awk 'BEGIN {min = 6553565} {if ($1<min) min=$1 fi} END {print "Min=", min}'
求时延
cat 1.txt |awk '{if($1=="0") ++sum1;if($1 >="1" && $1 <"10") ++sum2}END{print "0ms"" "sum1"
""<10ms"" "sum2}'