Skip to content

Instantly share code, notes, and snippets.

@liviaerxin
Created December 18, 2021 09:37
Show Gist options
  • Save liviaerxin/2f374953d9cc3c24a99d6b175210304e to your computer and use it in GitHub Desktop.
Save liviaerxin/2f374953d9cc3c24a99d6b175210304e to your computer and use it in GitHub Desktop.
Get the Min, Max, Median, and Mean of a Dataset By awk
#!/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