Monday, May 14, 2012
human readable numbers, one liners
From human readable to absolute:
hr_to_abs() {
(( $# < 1 )) && return 1
[[ "$1" =~ '^[0-9]+[kKmMgGtT]?$' ]] || return 1
echo $(( $(echo $1 | sed '/[kKmMgGtT]$/ { s//*1024**&/; y/kKmMgGtT/11223344/; }') ))
}
To human readable, I cheated and used perl:
$ perl -npe '
> s!([0-9]+)!
> sprintf("%.2f",$1/1024**($exp=int(log($1)/log(1024)))).qw(K M G T)[$exp];!eg;
> ' <<< '123 1234 12345 123456 1234567 12345678 123456789'
123.00K 1.21M 12.06M 120.56M 1.18G 11.77G 117.74G
$
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment