If you try to average a large number with awk, it will overflow with digits.

Asked 1 years ago, Updated 1 years ago, 75 views

There are several days' worth of files retrieved from vmstat-a5.
In order to calculate the average memory usage, I would like to know the average usage of Free, but when I add it with awk, it overflows with digits and it doesn't work.

In the shell script,
I am.

VAR=`awk'{m+=$4}END{printm/NR;}'${myfile}`

Please let me know if there is a good way.

shellscript awk

2022-09-29 21:50

2 Answers

awk'{a+=($4-a)/NR}END {printa}'


2022-09-29 21:50

Notation Problems

To avoid displaying exponential notation (scientific notation), you can use printf to specify the format.

printf "%d\n",m;

You can also decide in advance which format print should be used by OFMT.

BEGIN {OFMT="%d"}

If your data is huge

The questioner's case this time was different, but
When calculating the average of huge data, the calculation method that accumulates into one floating-point number is inevitably a problem of accuracy.Even with the calculation method, it is easy to notice problems such as not reflecting the data in the second half.

In this case, it is easy to use something that can perform arbitrary precision operations (multiple-length operations) as others commented.
There is also a way to calculate the average after calculating the average for each period of time.


2022-09-29 21:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.