perl to parse out iostat information on solaris 9

Discussion in 'Perl Misc' started by inetquestion, Aug 9, 2007.

  1. inetquestion

    inetquestion Guest

    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";
     
    inetquestion, Aug 9, 2007
    #1
    1. Advertising

  2. inetquestion <> wrote:

    > 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);


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad McClellan, Aug 9, 2007
    #2
    1. Advertising

  3. inetquestion

    inetquestion Guest

    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
     
    inetquestion, Aug 9, 2007
    #3
  4. inetquestion

    -berlin.de Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > inetquestion <> wrote:


    [...]

    > 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
     
    -berlin.de, Aug 9, 2007
    #4
  5. -berlin.de wrote:
    > Tad McClellan <> wrote in comp.lang.perl.misc:


    >
    >>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.


    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
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
     
    Josef Moellers, Aug 9, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    19
    Views:
    1,169
    Daniel Vallstrom
    Mar 15, 2005
  2. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    206
    7stud --
    Feb 20, 2008
  3. Daniel Berger
    Replies:
    12
    Views:
    212
    ara.t.howard
    Aug 29, 2008
  4. Alice Stamp

    Load information in Solaris (Perl Script.)

    Alice Stamp, Nov 5, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    108
    Michele Dondi
    Nov 5, 2004
  5. Replies:
    1
    Views:
    135
    Tad McClellan
    Mar 26, 2007
Loading...

Share This Page