Skip to content

Instantly share code, notes, and snippets.

@stig
Last active February 4, 2022 14:55
Show Gist options
  • Save stig/c7a13879534126fbf56c to your computer and use it in GitHub Desktop.
Save stig/c7a13879534126fbf56c to your computer and use it in GitHub Desktop.
Print percentiles for list of data points
#!/bin/sh
# Exit immediately on error
set -e
# set -x
# Create temporary file
FILE=$(mktemp /tmp/$(basename $0).XXXXX) || exit 1
trap "rm -f $FILE" EXIT
# Sort entries
sort -n $* > $FILE
# Count number of data points
N=$(wc -l $FILE | awk '{print $1}')
# Calculate line numbers for each percentile we're interested in
P50=$(dc -e "$N 2 / p")
P90=$(dc -e "$N 9 * 10 / p")
P99=$(dc -e "$N 99 * 100 / p")
echo ";; 50th, 90th and 99th percentiles for $N data points"
awk "FNR==$P50 || FNR==$P90 || FNR==$P99" $FILE
@rdp
Copy link

rdp commented Jan 15, 2018

99th but not 1st/25th? :) also may want to delete that temp file after you'r done with it :|

@stig
Copy link
Author

stig commented Jan 15, 2018

99th but not 1st/25th?

Most requests were pretty quick, so 1st and 25th were not really useful at the time, but we had a few that were very anomalous. Hence the 90 and 99. You can of course change them to / or add whatever you need.

PS: Thanks for the suggestion of removing the temporary file. I've added a trap statement to take care of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment