?
Solved

sorting a list of numbers and strings

Posted on 2000-03-15
12
Medium Priority
?
174 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:orango
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 3

Expert Comment

by:guadalupe
ID: 2619628
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
 

Author Comment

by:orango
ID: 2619862
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
 
LVL 1

Expert Comment

by:builder110697
ID: 2620192
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 2620723
print sort{$a<=>$b} `du -k`;
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2621259
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
 
LVL 5

Expert Comment

by:PC_User321
ID: 2621655
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
 
LVL 84

Expert Comment

by:ozo
ID: 2621739
#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
 
LVL 5

Expert Comment

by:PC_User321
ID: 2621793
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
 
LVL 84

Expert Comment

by:ozo
ID: 2621819
print sort map{s/\s*(\d+)/sprintf("%10d", $1)/e;$_} `du -k`;
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2621839
Good.  Without even using a Schw...whatever :)

Just needs a {$b cmp $a} to round it off.
0
 

Author Comment

by:orango
ID: 2622911
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
 

Author Comment

by:orango
ID: 2622933
Works fine. Ideal solution thank you all for your help.

Im not sure how it works but Ill keep reading the perl books !
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question