For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > Appending a character to a field









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Appending a character to a field
bokjasong@gmail.com

2007-08-30, 3:59 am



Hi,


I'm a perl newbie and would like to ask this question here.

Let's say I have the following code. Trying to check the disk space,
it's to truncate the percent sign % from the df -k output, compare the
percentage field to see if it's bigger than 90%, and grasp only those
lines that are and push those to an array @df. But how can I add the
percentage sign back to the percentage field on each line for the
proper output? I put down ??????? as below to find out what regexp
needs to be there in the below.


$percent=90;

open(DFOUT, "/usr/bin/df -k|");
while (<DFOUT> ) {
next if ($_ =~ /Filesystem/i);
s/\%//; # to remove the percent sign for
comparison
if ( (split)[4] >= $percent ) {
???????
push(@df, $_);
}
}


print "@dfoutput\n";

I appreciate your help very much!

Thank you.


- Bok

Jeff Pang

2007-08-30, 3:59 am

Try to use this way,

use strict;
my $percent=90;
my @df;

open(DFOUT, "df -k|");
while (<DFOUT> ) {
next if /Filesystem/i;
my ($free) = /(\d+)%/;
if ( $free >= $percent ) {
push(@df, $_);
}
}
close DFOUT;

print "@df";



2007/8/30, bokjasong@gmail.com <bokjasong@gmail.com>:
>
>
> Hi,
>
>
> I'm a perl newbie and would like to ask this question here.
>
> Let's say I have the following code. Trying to check the disk space,
> it's to truncate the percent sign % from the df -k output, compare the
> percentage field to see if it's bigger than 90%, and grasp only those
> lines that are and push those to an array @df. But how can I add the
> percentage sign back to the percentage field on each line for the
> proper output? I put down ??????? as below to find out what regexp
> needs to be there in the below.
>
>
> $percent=90;
>
> open(DFOUT, "/usr/bin/df -k|");
> while (<DFOUT> ) {
> next if ($_ =~ /Filesystem/i);
> s/\%//; # to remove the percent sign for
> comparison
> if ( (split)[4] >= $percent ) {
> ???????
> push(@df, $_);
> }
> }
>
>
> print "@dfoutput\n";
>
> I appreciate your help very much!
>
> Thank you.
>
>
> - Bok
>
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com