1820882 Members
3415 Online
109628 Solutions
New Discussion юеВ

Perl: Sort

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Perl: Sort

I found this script that I like on these forums. I don't really know perl at all so I am wondering if someone here can help. How can I sort the output by the percentage. So the output would be something like this:

/home/usera (xxxxxxxkb) 45%
/home/userb (xxxxxxxkb) 20%
/home/userc (xxxxxxxkb) 5%

Thanks,

#!/usr/contrib/bin/perl
# Show the percentage of total used space in /home to each user
# 11/18/02 BRS

$home_thresh = 1 ; # won't print if it isn't more than 1%

# Get the size of each users home directory

# sub homes_report {
$home_total = `du -sk /home 2> \/dev\/null`;
($home_total,$nothing) = split(/\s+/,$home_total) ;


print "/home's total usage is $home_total kb \n" ;

foreach (`du -sk /home/* 2> \/dev\/null` ) {
chop ;
($home_kb,$home_dir) = split (/\s+/);
$homes_size{$home_dir} = $home_kb ;
$home_pct = int(($home_kb / $home_total) * 100) ;
if ( $home_pct > $home_thresh ) {
print $home_dir ($home_kb kb) is $home_pct% \n" ;
}
}
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor

Re: Perl: Sort

This is *very* old perl code. And bad style too

#!/opt/perl/bin/perl

use strict;
use warnings;

# Show the percentage of total used space in /home to each user

my $home_thresh = 1 ; # won't print if it isn't more than 1%

# Get the size of each users home directory

# sub homes_report {
(my $home_total = `du -sk /home 2> /dev/null`) =~ s/\s.*//s;

print "/home's total usage is $home_total kb \n" ;

my %homes;
for (`du -sk /home/* 2>/dev/null`) {
chomp;
my ($home_kb, $home_dir) = split m/\s+/;
$homes{$home_dir}{size} = $home_kb;
my $home_pct = int (($home_kb / $home_total) * 100);
$home_pct > $home_thresh or next;
$homes{$home_dir}{pct} = $home_pct;
}

foreach my $dir (sort { $homes{$b}{pct} <=> $homes{$a}{pct} } keys %homes) {
printf "%6.2f%% %s\n", $homes{$dir}{pct}, $dir;
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Perl: Sort

Vo tried and found a small glitch.
Try this instead

#!/opt/perl/bin/perl

use strict;
use warnings;

my $users = shift || "/users";

# Show the percentage of total used space in /home to each user
my $home_thresh = 1.; # won't print if it isn't more than 1%
# Get the size of each users home directory
# sub homes_report {
(my $home_total = `du -sk $users 2> /dev/null`) =~ s/\s.*//s;
print "$users\'s total usage is $home_total kb \n";
$home_total > 0 or exit;

my %homes;
for (`du -sk $users/* 2>/dev/null`) {
chomp;
my ($home_kb, $home_dir) = split m/\s+/;
$homes{$home_dir}{size} = $home_kb;
my $home_pct = int (($home_kb / $home_total) * 100);
$homes{$home_dir}{pct} = $home_pct;
}
foreach my $dir (sort { $homes{$b}{pct} <=> $homes{$a}{pct} } keys %homes) {
$homes{$dir}{pct} > $home_thresh and
printf "%6.2f%% %s\n", $homes{$dir}{pct}, $dir;
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Coolmar
Esteemed Contributor

Re: Perl: Sort

Thanks, that second one worked like a charm.

V. Nyga
Honored Contributor

Re: Perl: Sort

Merijn,

your a genius!!!!!!!!!

Many thanks and 25 pts. from me *1*

Best wishes
see you
Volkmar
*** Say 'Thanks' with Kudos ***