How Do I Locate a value in a text file and evaluate it and then write

How Do I Locate a value in a text file and evaluate it and then write

am 28.12.2007 19:11:33 von kwalike57

Hello.

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.

Thanks for your help.

Karin Walike

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

am 28.12.2007 19:48:42 von jurgenex

On 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%

Where's the problem? Loop through the file using a standard while(){}.
When you find a line with a content that is eq()ual to '%Used' then read the
next line and check if the value is > 60.

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

That depends on what that line actually contains.
Standard answer: if you know the separator between values, then use split(),
if you know the value then use m/(...)/.

It is impossible to give more detailed suggestions without seeing the actual
data.

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

That is nice, but a vague verbal description of the data can only result in
a vague verbal description of the algorithm.

jue

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

am 28.12.2007 20:06:56 von someone

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

Re: How Do I Locate a value in a text file and evaluate it and then

am 28.12.2007 20:16:38 von it_says_BALLS_on_your forehead

On Dec 28, 2:06=A0pm, "John W. Krahn" wrote:
> 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. =A0The 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> =3D~ /%used?\b|\bused?%/i and my $pos =3D $-[0];
>
> /\A.{$pos}\s*(\d+)/ && $1 > 60 && print while <$ph>;
>
> close $ph or warn $! ? "Error closing 'df' pipe: $!"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 : "Exit status $? from 'df'";
>

That's pretty nifty (no sarcasm intended). However, have you tried
it?
I don't get what I expect. Perhaps the issues are on my end though...

bash-2.03$ ./df.pl
df: cannot statvfs /foswbdmmk1/install: Permission denied
df: cannot statvfs /foswbdmmk1/admin: Permission denied
df: cannot statvfs /foswbdmmk1/was: Permission denied
df: cannot statvfs /foswbdmmk1/stag: Permission denied
df: cannot statvfs /foswbdmmk1/prev: Permission denied
df: cannot statvfs /foswbdmmk1/log: Permission denied
df: cannot statvfs /foswbdmmk1/ihs: Permission deniedUse of
uninitialized value in concatenation (.) or string at ./df.pl line 10,
<$ph> line 2.

df: cannot statvfs /foswbdmmk1/app: Permission denied
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 3.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 4.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 5.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 6.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 7.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 8.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 9.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 10.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 11.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 12.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 13.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 14.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 15.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 16.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 17.
Use of uninitialized value in concatenation (.) or string at ./df.pl
line 10, <$ph> line 18.

Re: How Do I Locate a value in a text file and evaluate it and then

am 02.01.2008 22:48:09 von kwalike57

On Dec 28 2007, 10:48=A0am, Jürgen Exner wrote:
> On 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. =A0The header %Used is located on the line above the actual
> >value, like this:
>
> >%Used
> >60%
>
> Where's the problem? Loop through the file using a standard while(){}.
> When you find a line with a content that is eq()ual to '%Used' then read t=
he
> next line and check if the value is > 60.
>
> >How would I capture and evaluate only the %Used value and no other
> >values in that line?
>
> That depends on what that line actually contains.
> Standard answer: if you know the separator between values, then use split(=
),
> if you know the value then use m/(...)/.
>
> It is impossible to give more detailed suggestions without seeing the actu=
al
> data.
>
> >The 60% occurs first in the value line followed by a percent value for
> >inodes used.
>
> That is nice, but a vague verbal description of the data can only result i=
n
> a vague verbal description of the algorithm.
>
> jue

Hello.

Thanks for the note.

Here is additional information:

Sample file excerpt...

Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/hd4 131072 38912 71% 4261 32% /
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/inputlv 255328256 118925432 54% 564 1% /IPW/input

=46rom this example, I want to evaluate the column labeled %Used for
values greater than 60% and if that value is greater than 60%, I then
want to write that entire line, (/dev/hd4 131072 38912
71% 4261 32% /
) out to a text file that will be emailed out to report on filesystems
that are now at a capacity of 60% or greater.

I already have the script written to create the original df -k output
to a text file and create the email generation. I just need to have
help evaluating the correct values in the original df -k output file
and then creating the new file containing entries of all filesystems
that are at or above 60% capacity.

Does this help clarify my question?

Thanks again,

Karin Walike

Re: How Do I Locate a value in a text file and evaluate it and then

am 02.01.2008 22:48:55 von kwalike57

On Dec 28 2007, 11:06=A0am, "John W. Krahn" wrote:
> 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. =A0The 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> =3D~ /%used?\b|\bused?%/i and my $pos =3D $-[0];
>
> /\A.{$pos}\s*(\d+)/ && $1 > 60 && print while <$ph>;
>
> close $ph or warn $! ? "Error closing 'df' pipe: $!"
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 : "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. =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0-- =
Larry Wall- Hide quoted text -
>
> - Show quoted text -

Hello.

Thanks for the note.

Here is additional information:

Sample file excerpt...

Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/hd4 131072 38912 71% 4261 32% /
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/inputlv 255328256 118925432 54% 564 1% /IPW/input

=46rom this example, I want to evaluate the column labeled %Used for
values greater than 60% and if that value is greater than 60%, I then
want to write that entire line, (/dev/hd4 131072 38912
71% 4261 32% /
) out to a text file that will be emailed out to report on filesystems
that are now at a capacity of 60% or greater.

I already have the script written to create the original df -k output
to a text file and create the email generation. I just need to have
help evaluating the correct values in the original df -k output file
and then creating the new file containing entries of all filesystems
that are at or above 60% capacity.

Does this help clarify my question?

Thanks again,

Karin Walike

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

am 03.01.2008 01:20:09 von Jim Gibson

In article
,
kwalike57 wrote:


> Sample file excerpt...
>
> Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
> /dev/hd4 131072 38912 71% 4261 32% /
> Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
> /dev/inputlv 255328256 118925432 54% 564 1% /IPW/input
>
> From this example, I want to evaluate the column labeled %Used for
> values greater than 60% and if that value is greater than 60%, I then
> want to write that entire line, (/dev/hd4 131072 38912
> 71% 4261 32% /
> ) out to a text file that will be emailed out to report on filesystems
> that are now at a capacity of 60% or greater.
>
> I already have the script written to create the original df -k output
> to a text file and create the email generation. I just need to have
> help evaluating the correct values in the original df -k output file
> and then creating the new file containing entries of all filesystems
> that are at or above 60% capacity.

Here is a very simple program to print lines with 60% or greater %Used:

#!/usr/local/bin/perl
use strict;
use warnings;

while() {
if( /(\d+)%/ ) {
print if $1 >= 60;
}
}
__DATA__
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/hd4 131072 38912 71% 4261 32% /
Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
/dev/inputlv 255328256 118925432 54% 564 1% /IPW/input


You can modify this program to write out the lines to a text file
instead of printing them.

You can also do most of your processing in Perl, including running and
capturing the output of the df command:

my @usage = `df k`;

and there are Perl modules available on CPAN (
that can email, or see 'perldoc -q mail' "How do I send mail?"

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

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

am 03.01.2008 02:02:04 von Tad J McClellan

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.

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


Match the first run of digits that is followed by a percent sign.


-------------------
#!/usr/bin/perl
use warnings;
use strict;

foreach ( qx/df -k/ ) {
print if /(\d+)%/ and $1 >= 60;
}
-------------------


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

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

am 03.01.2008 19:20:42 von jurgenex

kwalike57 wrote:
>Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
>/dev/hd4 131072 38912 71% 4261 32% /
>Filesystem 1024-blocks Free %Used Iused %Iused Mounted on
>/dev/inputlv 255328256 118925432 54% 564 1% /IPW/input
>
>From this example, I want to evaluate the column labeled %Used for

Ok, that would be the fourth column.

>values greater than 60% and if that value is greater than 60%, I then
>want to write that entire line, (/dev/hd4 131072 38912
>71% 4261 32% /

Assuming the the data is stored in @t:

for (@t) {
print if ((split)[3] >= 60);
}

Note: this is not warning safe, so you will have to disable warnings for
this line.

>) out to a text file that will be emailed out to report on filesystems
>that are now at a capacity of 60% or greater.

Above you wrote greater than 60% and here 60% or greater. Which one is it?

>I already have the script written to create the original df -k output
>to a text file and create the email generation.

Why writing the df output to a file instead of storing it in an array? That
output can't be that large.
Anyway, if you want to use that temporary file and process it line by line
instead (which IMO is totally unnecessary in this case) then use the
standard
while()
loop instead of the for() loop.

jue