perl to parse out iostat information on solaris 9

I

inetquestion

For the program below why are all 4 attributes not getting filled in.
The data seems to be coming across per the Debug statement...

btw - I am running this on solaris 9. The problem seems to be related
to the whitespace at the beginning of the line, but I thought the
regex should account for it...

-Inet

#!/usr/bin/perl

chomp($CPU=`iostat -c 1 2 | tail -1`);
($usrcpu,$syscpu,$wiocpu,$idlecpu) = split(/\s+/,$CPU);

print "DEBUG: $CPU\n";
print "usrcpu: $usrcpu\n";
print "syscpu: $syscpu\n";
print "wiocpu: $wiocpu\n";
print "idlecpu: $idlecpu\n";
 
T

Tad McClellan

inetquestion said:
For the program below why are all 4 attributes not getting filled in.
The data seems to be coming across per the Debug statement...


What is "the Debug statement"?

btw - I am running this on solaris 9.


If you show what the data looks like, then even people who are not
on Solaris could attempt to help you.

I have no idea what the output of iostat looks like.

The problem seems to be related
to the whitespace at the beginning of the line,


Then one solution would be to remove the whitespace at the beginning
of the line before split()ting the string.

$CPU =~ s/^\s+//;

but I thought the
regex should account for it...


The 2nd sentence of the documentation for the function you are using says:

By default, empty leading fields are preserved

You have an empty leading field.

($usrcpu,$syscpu,$wiocpu,$idlecpu) = split(/\s+/,$CPU);


Another solution would be to discard the empty leading field:

(undef, $usrcpu, $syscpu, $wiocpu, $idlecpu) = split(/\s+/, $CPU);
 
I

inetquestion

Tad,

The output for the debug line appears as follow:

DEBUG: 1 2 3 94

As you mentioned using a 5th variable or trimming the leading space
would work. I guess I got caught up thinking that regex should do the
job with no further modification needed. Is there another one I could
have used instead which would dump leading spaces?

-Inet
 
A

anno4000

Tad McClellan said:
[...]

I have no idea what the output of iostat looks like.

Nor do I.
The problem seems to be related
to the whitespace at the beginning of the line,


Then one solution would be to remove the whitespace at the beginning
of the line before split()ting the string.

$CPU =~ s/^\s+//;
[...]

Another solution would be to discard the empty leading field:

(undef, $usrcpu, $syscpu, $wiocpu, $idlecpu) = split(/\s+/, $CPU);

That is only a solution if the empty field is guaranteed to be present.
Since we don't know the format of the line, that's not a given.

Anno
 
J

Josef Moellers

That is only a solution if the empty field is guaranteed to be present.
Since we don't know the format of the line, that's not a given.

In these circumstances (parsing /proc/interrupts on a Linux machine),
I've done it like this:

my @f = split(/\s+/, $line);
shift @f unless length $f[0];

Josef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top