Today on the Internet to see a web connection average time of the shell command, in their own machine tried, found that can not use, incredibly appear awk:fatal:division by zero attempted such a mistake, Mao's own changed the shell command.
Original Shell Script Example:
Copy Code code as follows:
Cat access.log|grep "Connect CBP" |awk ' begin{sum=0;count=0;} {sum+=$10;count++;} end{printf ("sum=%d,count=%d,avg=%f\n", Sum,count, Sum/count)} '
Modified Shell:
Copy Code code as follows:
Cat Access.log |awk ' {sum+=$10;count+=1} end{print ' sum: ' Sum ' \navg: ' Sum/count ' \ncount: ' Count} '
Execution results:
Copy Code code as follows:
sum:33403166
avg:7951.24
count:4201
Of course, directly change the original can also:
Copy Code code as follows:
Cat Access.log |awk ' begin{sum=0;count=0;} {sum+=$10;count++;} end{printf ("sum=%d,count=%d,avg=%f\n", Sum,count, Sum/count)} '
Execution results:
Copy Code code as follows:
sum=33403166,count=4201,avg=7951.241609
You can see that the original shell script asks for more accurate results. If you need anything, you can try it yourself.