More
author |
Steve Losh <steve@stevelosh.com> |
date |
Wed, 27 Mar 2024 15:17:47 -0400 |
parents |
ca4e18cd273a |
children |
(none) |
#!/usr/bin/env bash
set -euo pipefail
function usage {
echo "hist - display a histogram of stdin, bucketed by unique lines
USAGE: $0 [OPTIONS]
Note that the output lines will be unsorted. For sorted output you can pipe the
output to sort:
cat foo.dat | hist | sort -n -k3
Options:
-h, --help display this help text and exit
-w COLS, --width COLS how wide the histogram bars should be
"
}
function die {
echo "$0: $1" >&2;
exit 1
}
OPTS=$(getopt --options hw: --long help,width: --name "$0" -- "$@")
eval set -- "$OPTS"
WIDTH=40
while test "$#" -gt 0; do
case "$1" in
-h | --help ) usage; exit 0;;
-w | --width ) WIDTH=$2; shift 2 ;;
-- ) shift; if test "$#" -ne 0; then die "unrecognized arguments: $*"; fi ;;
* ) die "unrecognized arguments: $*" ;;
esac
done
props | awk -v width="$WIDTH" '
{
n = $1
$1 = ""
key = substr($0, 1)
vals[key] = n
if (n > max) max = n
}
END {
x = 1 / max
for (key in vals) {
val = vals[key]
bars = int(val * x * width)
pads = width - bars
printf "%s |", val
for(i=0; i<bars; i++) printf "█"
for(i=0; i<pads; i++) printf " "
print key
}
}
'