Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
A humble offering, hoping to find a place where its perlishness won't be scorned. On another site, I got a little obsessed with this question I came across: How can I get sorted, human-readable output from du? It doesn't have a sort option, so you have to pass it to another program, like sort:
du | sort -nr
But if you want human-readable output, you can't do that, because sort can't sort it numerically. Others were offering shell solutions that invoked du on each item twice, once before being passed through sort, and once after for the human-readable verison. Wasteful! After finding solution in Awk, I wrote this "one-liner":
du -B1 | sort -nr | perl -e '@h=qw(b K M G);for(<>){($s,@f)=split/\s+/ +;$e=3;$e-- while(1024**$e>$s);$v=($s/(1024**$e));printf "%-8s%s\n",sp +rintf($v>=100?"%d%s":"%.1f%s",$v,$h[$e]),@f;}'
But then I realized it'd probably be better to use Perl for the sorting, and got this shorter result:
du -h | perl -e 'sub h{%h=(K=>10,M=>20,G=>30);($n,$u)=shift=~/([0-9.]+ +)(\D)/;return $n*2**$h{$u}}print sort{h($b)<=>h($a)}<>;'
Both of them return human-readable disk usage output, sorted largest item first:
4.4M . 3.6M ./colors 372K ./plugin 128K ./autoload 100K ./syntax 100K ./doc
I almost posted this to the 'Obfuscation' area, but I thought this would be more appropriate. I am prepared to do penance if I was mistaken.

Update: Thanks to hbm, grinder, and Roy Johnson, a significantly smaller version has been golfed (putting it here because it's buried around ^7 in the comments):
perl -e'%h=map{/.\s/;99**(ord$&&7)-$`,$_}`du -h`;die@h{sort%h}'

In reply to du -h, sorted by bellaire

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (6)
    As of 2014-10-31 04:15 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (214 votes), past polls