Created
December 18, 2021 09:37
-
-
Save liviaerxin/2f374953d9cc3c24a99d6b175210304e to your computer and use it in GitHub Desktop.
Get the Min, Max, Median, and Mean of a Dataset By awk
This file contains hidden or 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
#!/usr/bin/env awk -f | |
# Get the Min, Max, Median, and Mean of a Dataset | |
NR==1 { max=$1; min=$1 } | |
{ | |
sum += $1 | |
sumsq += ($1)^2 | |
$1 < min ? min = $1 : 0 | |
$1 > max ? max = $1 : 0 | |
} | |
END { | |
if (NR == 0) exit #To avoid division by zero | |
mean = sum/NR | |
sd = sqrt(sumsq/NR - mean^2) | |
#Let's beautify the output | |
printf \ | |
"min = %s, max = %s,mean = %s, standard deviation = %s\n",\ | |
min,\ | |
max,\ | |
mean,\ | |
sd | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment