sorting a list of numbers and strings

Im trying to sort the output of a df -k top down. The problem is each line is a mix of a number then a tab and then the folder ie
1234      /usr/home/somebody
If I sort it, it does it alphabetically as they contain strings. I think if I use split to split them I can then sort the numbers but I want to keep a reference to the string part so the numbers and folders tally on output.
Any help appreciated.
orangoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ozoConnect With a Mentor Commented:
print sort{$a<=>$b} `du -k`;
0
 
guadalupeCommented:
Try this...the out put is a little ugly but you can re do it...

#!/usr/local/bin/perl

$lines = `du -k`;



@lines = split(/\n/, $lines);

%dus = map{(split/\s+/)[0,1]}@lines;

@keys = sort { $a <=> $b} (keys %dus);

foreach $key (@keys)
{
      print "$key = $dus{$key}\n";
}
0
 
orangoAuthor Commented:
Im not brilliant at perl Im afraid and don't fully understand the code.It is virtually there but if file sizes happen to be the same it only reports back the last entry at that size. Im guessing that the filesize is being used as the key to the hash dus and overites the previous entry if it happens to be the same.

In this line can you tell me what the
[0,1] does. Is it part of the split or map.

%dus = map{(split/\s+/)[0,1]}@lines;

Thanks for the help
Regards
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
builder110697Commented:
Try this.  It works for me.


#!/bin/perl

my $homedirs = "/usr/home";
open( TTT, "du -sk $homedirs/* |" );
foreach ( <TTT> ) {
  chomp;
  @tmp = split( /       /, $_ );
  $tmp[1] =~ s/\/..*\///;
  $diskspace{$tmp[1]} = $tmp[0];
}
close TTT;

print "  Username        Diskspace\n ----------      -----------\n";
foreach ( sort keys %diskspace ) {
  printf( "  %-12s %12d\n", $_, $diskspace{$_});
}
0
 
PC_User321Commented:
I don't understand why you object to alphabetic sorting, because, since the sizes are presumably right aligned, an alphabetic sort will work fine.
This will sort alphabetically, top down:

print sort{$b cmp $a} `du -k`;

To do a _numeric_ sort, you have to isolate the numbers and sort according to them.
The line below extracts the size by using /^\s*(\d+)/, then sorts numerically (top down) using that as the key.

print sort{$b =~ /^\s*(\d+)/ <=> $a =~ /^\s*(\d+)/} `du -k`;
0
 
PC_User321Commented:
Embarrassment!  
I did not test my earlier post.  Now I understand why alphabetical sort does not work.

The simplest solution is ozo's, modified for top down:
   print sort{$b<=>$a} `du -k`;

It complains about non-numeric values being used in a numeric comparison, but it works.

To clean it up you need to isolate the numeric part:
   print sort{($A = $a) =~ /\d+/; ($B = $b) =~ /\d+/; $B <=> $A} `du -k`;
0
 
ozoCommented:
#Or turn off the warnings
{local $^W=0;print sort{$b <=> $a} `du -k`}

#(if you need to do more processing, like ($A = $a) =~ /\d+/ it may be worth using a Schwartzian Transform)
0
 
PC_User321Commented:
For fun I tried a 1-liner that produces formatted output.
This works 98%
   print sort{$b cmp $a} map{s/(\d+)/sprintf("%10d", $1)/e, $_} `du -k`;

Perhaps someone could provide the missing 2%
0
 
ozoCommented:
print sort map{s/\s*(\d+)/sprintf("%10d", $1)/e;$_} `du -k`;
0
 
PC_User321Commented:
Good.  Without even using a Schw...whatever :)

Just needs a {$b cmp $a} to round it off.
0
 
orangoAuthor Commented:
I copied your program but when I run it. It doesn't work correctly it will only print this
devel:~ # ./test.pl
  Username        Diskspace
 ----------      -----------
                          8
where as du -sk /home/* gives

1816    /home/admin
4       /home/bill
8       /home/bob

when run with -w gives

Use of uninitialized value at ./test.pl line 7, <TTT> chunk 3.
Use of uninitialized value at ./test.pl line 8, <TTT> chunk 3.
Use of uninitialized value at ./test.pl line 8, <TTT> chunk 3.
Use of uninitialized value at ./test.pl line 7, <TTT> chunk 3.
Use of uninitialized value at ./test.pl line 8, <TTT> chunk 3.
Use of uninitialized value at ./test.pl line 7, <TTT> chunk 3.
Use of uninitialized value at ./test.pl line 8, <TTT> chunk 3.
  Username        Diskspace
 ----------      -----------
Argument "8^I/home/bob" isn't numeric in prtf at ./test.pl line 14.
                          8

0
 
orangoAuthor Commented:
Works fine. Ideal solution thank you all for your help.

Im not sure how it works but Ill keep reading the perl books !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.