How to calculate the average of value in lines of a text file on Linux by a script?

Posted on In QA

How to calculate the average of value in lines of a text file on Linux by a script?

The file.txt is like:

Run 0: Time used: 22.235331711 seconds.
Run 1: Time used: 20.784491219 seconds.
Run 2: Time used: 21.851638876 seconds.

What I want it to calculate the average time.

awk is handy for this purpose:

awk '{ total += $5; count++ } END { print total/count }' file.txt

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *