Last active
August 29, 2015 14:01
-
-
Save kofemann/56de59169e98f4708dbe to your computer and use it in GitHub Desktop.
Calculating Running Average with AWK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BEGIN { | |
avg = 0.0 | |
sigma = 0.0 | |
} | |
{ | |
v = $1 | |
avg = (avg*(NR - 1) + v) / NR | |
sigma = (sigma*(NR - 1) + v*v) / NR | |
} | |
END { | |
printf("Avg: %.2f, sigma: %.2f, total %.2f with %d records\n", | |
avg, sqrt(sigma - avg*avg), avg*NR, NR) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment