1756758 Members
2640 Online
108852 Solutions
New Discussion юеВ

Using df -k on Perl

 
SOLVED
Go to solution
Steve_617
Advisor

Using df -k on Perl

I have sent a df -k to a tmp1 file
I then try and loop through the file and send each line to an aray.
I then want to print out the % field and do a test to see if the disk space is greater than 80% so I can perform a task on that, but for some reason when I split the array using qw(@aray_name), it does not give me any data.

Any advice
12 REPLIES 12
H.Merijn Brand (procura
Honored Contributor

Re: Using df -k on Perl

qw(@array) will return a list with ONE element: '@array'

qw// will Quote Words: any sequence of non blanks is considered a word in that, except the ',' for which you will probably get a warning

what you probably meant was to split up each line into fields, where the fields are seperated by spaces:

my @list = split /\s+/;

In a more full context:

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5] > 80 and print;
}

HTH, Enjoy, Have FUN! H.Merijn

Enjoy, Have FUN! H.Merijn
Steve_617
Advisor

Re: Using df -k on Perl

Thanks

I have assigned the array now to a second array and then substituted the % sign with //.
This works, but is there a simpler and quicker way to do so.
I get an error message that 46% is not a numeric value.
H.Merijn Brand (procura
Honored Contributor

Re: Using df -k on Perl

That's GOOD! You are using warnings!
That is a bonus for you.

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5] =~ m/(\d+)/ && $1 > 80 and print;
}

is a cleaner way, or you can use the less maintainable way of looking at the value in string context:

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5] =~ m/^([89]\d|1\d\d)%$/ and print;
}

this will match all percentages from 80 and up.

FYI the *dirty* way to get rid of this warning (since you know it is OK) is to (temporarily) disable warning:

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
no warnings;
$f[5] > 80 and print;
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve_617
Advisor

Re: Using df -k on Perl

Why can I not do the following

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5]=~tr/\%//g and print; ### my changes
}


My goal is to have an output
/usr 88%
/var 94%
H.Merijn Brand (procura
Honored Contributor

Re: Using df -k on Perl

If that's your goal, why not simply

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
print "@f[0,5]\n";
}

or

local @ARGV = ("df -k |");
while (<>) {
print +(split/\s+/)[0,5],"\n";
}

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

Re: Using df -k on Perl

that 0,5 should be 1,5 in your case, where you want to see the mount point, and not the locical volume.

From the command line, that might even shorter look like:

a5:/u/usr/merijn 105 > df -k | perl -anle'print"@F[1,5]"'
Mount %used
/ 17%
/data 57%
/home 8%
/opt 95%
/pro 97%
/stand 26%
/tmp 8%
/u 50%
/usr 86%
/var 13%
/wrk 67%
a5:/u/usr/merijn 106 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve_617
Advisor

Re: Using df -k on Perl

Just to come back to my previous question.
Why can I not do the following

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5]=~tr/\%//g > 80 and print; ### my changes
}

I would like to print all the parttions greater than 80% but in the format
/usr 80%
/var 92%
H.Merijn Brand (procura
Honored Contributor
Solution

Re: Using df -k on Perl

firstly because tr does not give you back the value of the variable changed, but it returns the number of characters changed
secondly because tr/// is not the same as s/// and tr/// does not recognize the /g option (tr/// or the equivalent y/// only know of the options 'c' - complement the search list, 'd' - delete found but unreplaced characters, and 's' - squash duplicate replaced characters.

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5]=~tr/\%//g > 80 and print; ### my changes
}

that's why I gave you the second example

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5] =~ m/(\d+)/ && $1 > 80 and print;
}

but if you prefer to delete the '%' from the original string, you'd better do

local @ARGV = ("df -k |");
while (<>) {
my @f = split /\s+/;
$f[5] =~ tr/%//d && $f[5] > 80 and print;
}

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve_617
Advisor

Re: Using df -k on Perl

Thanks for all your help.
The first to responses gave me an option to give your help a rating, however I the last few replies there is no option to rate your responses..

If I may ask you what does the pipe do after df -k |.

I have just done a PErl course at HP but it seems I still have a long way to go, as I don't recognise most of the stuff you have shown me

Regards
Steve