Last active
December 2, 2021 13:59
-
-
Save roblogic/9e6ab27c57d26da642f3d7ab2d236ae1 to your computer and use it in GitHub Desktop.
istats - bash script to get various statistics from numerical data on stdin
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
#!/bin/bash | |
# iStats - get various statistics from numerical data on stdin | |
# aka "incredibly easy Stats" aka. "Stats for idiots" ;) | |
# fork of "jstats2" https://gist.github.com/roblogic/da238766f5e645ddfb7290b06b1f80f0 | |
set -e # Exit immediately on error | |
# set -x # Debug | |
usage="<input-stream> | istats" | |
# Create temporary file | |
WORK=$(mktemp /tmp/$(basename $0).XXXXX) || exit 1 | |
sort -n $* > $WORK | |
# Count data points | |
N=$(wc -l < $WORK) | |
[ $N -eq 0 ] && { echo "$usage" ; exit 1 ; } | |
echo " --stats analysis--" | |
echo " count n $N" | |
# Find percentile locations | |
P50=$(dc -e "$N 2 / p") | |
P90=$(dc -e "$N 9 * 10 / p") | |
P95=$(dc -e "$N 95 * 100 / p") | |
P99=$(dc -e "$N 99 * 100 / p") | |
# Print percentile values etc | |
echo " median $(awk "FNR==$P50" $WORK)" | |
echo " p90 $(awk "FNR==$P90" $WORK)" | |
echo " p95 $(awk "FNR==$P95" $WORK)" | |
echo " p99 $(awk "FNR==$P99" $WORK)" | |
echo -e " mean \u03BC $(awk '{sum+=$1}END{if (NR>0) print sum/NR}' $WORK)" | |
echo -e "stddev \u03C3 $(awk '{x+=$0;y+=$0^2}END{print sqrt(y/NR-(x/NR)^2)}' $WORK)" | |
echo " min $(awk "FNR==1" $WORK)" | |
echo " max $(awk "END{print}" $WORK)" | |
echo "datafile $WORK" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Example
$ seq 999 | istats --stats analysis-- count n 999 median 499 p90 899 p95 949 p99 989 mean μ 500 stddev σ 288.386 min 1 max 999 datafile /tmp/istats.E4Gd0