Note that there is also a -h option for sort on some systems. For those that lack it I have been using this code, piping the du -h output to it. The %so look-up hash gives to sort order for the scale suffix and allows for systems that use no suffix for bytes as well as those that use "B" instead.

use strict; use warnings; use Getopt::Std; my %opts = ( a => 0 ); getopts( q{a}, \ %opts ) or die qq{\n}; my %so = ( q{} => 0, B => 0, K => 1, M => 2, G => 3, T => 4, E => 5, P => 6, Z => 7, Y => 8, ); my $rcSortAscending = sub { my( $raA, $raB ) = @_; return $so{ $raA->[ 2 ] } <=> $so{ $raB->[ 2 ] } || $raA->[ 1 ] <=> $raB->[ 1 ] }; print map { my( $line, $path ) = @$_[ 0, 3 ]; if ( $path !~ m{/$} and -d $path and not -l $path ) { $line =~ s{\n}{/\n}; } $line; } sort { $opts{ a } ? $rcSortAscending->( $a, $b ) : $rcSortAscending->( $b, $a ) } map { [ $_, m{^\s*([\d.]+)([BKMGTEPZY]?)\s+(.*)} ] } <>;

I hope this is helpful.

Cheers,

JohnGG


In reply to Re: sort file with your own logic by johngg
in thread sort file with your own logic by citi2015

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.