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.


In reply to Re: sort du -sH output to find that disk hog by Aristotle
in thread sort du -sH output to find that disk hog by Plankton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.