Skip to content

Instantly share code, notes, and snippets.

@fsteffenhagen
Created November 19, 2021 09:30
Show Gist options
  • Save fsteffenhagen/45a33a76d75776882b439109bee1a2dd to your computer and use it in GitHub Desktop.
Save fsteffenhagen/45a33a76d75776882b439109bee1a2dd to your computer and use it in GitHub Desktop.
Calculate simple stats for an unsorted input of numerical values using awk.
# stats.awk
# Calculate simple stats for an unsorted input of numerical values using awk.
# Prints the Count, Minimum, Maximum, Average, Median, Sum of the input values.
#
# Usage:
# > gen_random.sh | awk -f stats.awk
# Count: 100
# Min: 0.202
# Max: 1647.48
# Avg: 280.658
# Mdn: 90.27
# Sum: 28575.7
# median calculates the Median of an array of numbers
function median(samples) {
len = asort(samples);
if(len % 2 == 1) {
return samples[int(len/2) + 1]
} else {
return samples[NR/2]
}
}
# processing first line of input
NR == 1 {
# init min
min = $1
# init max
max = $1
# init sum
sum = $1
# init all value array
values[NR] = $1
}
# processing subsequent input lines
NR > 1 {
# check for new min
min = (min < $1) ? min : $1
# check for new max
max = (max > $1) ? max : $1
# add current value to sum
sum += $1
# add current value to all value array
values[NR] = $1
}
# Print summary at the end
END {
printf("Count: %s\n", NR)
printf("Min: %s\n", min)
printf("Max: %s\n", max)
printf("Avg: %s\n", sum/NR)
printf("Mdn: %s\n", median(values))
printf("Sum: %s\n", sum)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment