Skip to content

Instantly share code, notes, and snippets.

View vivekmittal's full-sized avatar

Vivek Mittal vivekmittal

View GitHub Profile
@lewisd32
lewisd32 / percentile.sh
Last active October 3, 2024 13:07
Calculate percentile in bash
#!/bin/bash
# stdin should be integers, one per line.
percentile=$1
tmp="$(tempfile)"
total=$(sort -n | tee "$tmp" | wc -l)
# (n + 99) / 100 with integers is effectively ceil(n/100) with floats
count=$(((total * percentile + 99) / 100))
head -n $count "$tmp" | tail -n 1
rm "$tmp"