Skip to content

Instantly share code, notes, and snippets.

@kofemann
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kofemann/56de59169e98f4708dbe to your computer and use it in GitHub Desktop.
Save kofemann/56de59169e98f4708dbe to your computer and use it in GitHub Desktop.
Calculating Running Average with AWK
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