bin/hist @ 18cc4d4f4ac4

More
author Steve Losh <steve@stevelosh.com>
date Fri, 18 Sep 2020 17:21:49 -0400
parents d9ee2c4aa85b
children ca4e18cd273a
#!/usr/bin/env bash

set -euo pipefail

function usage {
    echo "hist - display a histogram of stdin, bucketed by unique lines

USAGE: $0 [OPTIONS]

Options:
  -h, --help       display this help text and exit
  -w N, --width N  how wide the histograms 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
    }
}
'