Why jump through so many hoops to infer the relative order of two sizes with a unit each when it would be so much easier to postpone the humanreadablification?
$ du -s | sort -rn | perl -ple'sub humanreadable { my ($s, $q, $u) = (
+$_[0], 1, 0); $s/=$q, $q*=1024, $u++ while $s > 1024; return sprintf
+"%.3g%s", $s, (" ", "K", "M", "G", "T")[$u] } s/^(\d+)\s+/sprintf "%1
+0s ", humanreadable($1)/e;'
Ok, you'll probably want to stick that code in a script at this point.
#!/usr/bin/perl -pl
use strict;
use warnings;
sub humanreadable {
my ($s, $q, $u) = ($_[0], 1, 0);
$s /= $q, $q *= 1024, $u++ while $s > 1024;
return sprintf "%.3g%s", $s, (" ", "K", "M", "G", "T")[$u];
}
s/^(\d+)\s+/sprintf "%10s ", humanreadable($1)/e;
This could probably stand a little parametrization à la sort(1) so it can be told which field in its input to humanreadablify.
On an unrelated note, I'm surprised that there doesn't seem to be a module on CPAN to format byte sizes in human readable form.
Makeshifts last the longest.
|