Shell calculates average connection time function based on Web log _linux shell

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.