Solved

sorting a list of numbers and strings

Posted on 2000-03-15
12
142 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
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 3

Expert Comment

by:guadalupe
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 84

Accepted Solution

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

Expert Comment

by:PC_User321
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
#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
Comment Utility
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
Comment Utility
print sort map{s/\s*(\d+)/sprintf("%10d", $1)/e;$_} `du -k`;
0
 
LVL 5

Expert Comment

by:PC_User321
Comment Utility
Good.  Without even using a Schw...whatever :)

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

Author Comment

by:orango
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 122
transpose into pipe delemited 8 65
iSeries PERL Scripts 7 126
Perl passing in variables to do substitution 6 60
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now