Re: How Do I Locate a value in a text file and evaluate it and then write out that line based on the value?



kwalike57 wrote:

I need to use Perl to read through log output from a df -k command on
a Unix server, for each line in the log where the value for %Used is
greater than 60% I need to write that line of the log out to another
text file. The header %Used is located on the line above the actual
value, like this:

%Used
60%

How would I capture and evaluate only the %Used value and no other
values in that line?

The 60% occurs first in the value line followed by a percent value for
inodes used.


open my $ph, '-|', 'df', '-k' or die "Cannot open pipe from 'df' $!";

<$ph> =~ /%used?\b|\bused?%/i and my $pos = $-[0];

/\A.{$pos}\s*(\d+)/ && $1 > 60 && print while <$ph>;

close $ph or warn $! ? "Error closing 'df' pipe: $!"
: "Exit status $? from 'df'";




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages