#!/bin/sh
#
# human - convert sizes to human-readable sizes

[ "$#" -eq 0 ] && set --
input="$1"
[ -z "${input}" ] && read -r input

echo "${input}" | awk \
  'function human(x) {
    if (x<1000) {return x} else {x/=1024}
    s="kMGTEPZY";
    while (x>=1000 && length(s)>1)
        {x/=1024; s=substr(s,2)}
    return int(x+0.5) substr(s,1,1)
}
{sub(/^[0-9]+/, human($1)); print}'
