Regex help - need flexibility to parse with or without blanks in output....

Discussion in 'Perl Misc' started by tudmuf2b@onebox.com, Dec 16, 2004.

  1. Guest

    Perl Gurus:

    I'm trying to write a script that runs the rup command, and then takes
    the output of it to do something.

    Here are 2 possible outputs that rup could give you (if a box has been
    up for less than a day, there is no "xx days" field):


    bastion1 up 18:01, load average: 0.17 0.15 0.06
    bastion2 up 30 days, 22:34, load average: 0.00 0.00 0.02


    My script works for the 2nd case (the "bastion2" host) - even if "days"
    is singular. But how do I make my regex more flexible so that it can
    say:

    1. if the box has been up for less than a day, then just skip to the
    time field and grab it, so that $days=0, $time=18:01 (from the example
    above).

    2. if the box has been up for X days, then I want to grab the number of
    days so that $days=30, and also grab the time, so that $time=22:34
    (above).

    Somehow I need to say something like
    /.*up\s+(\d+\s+days?,|.*\d{1,2}:\d+),/

    but I want to grab $days no matter what:



    #!/usr/bin/perl

    use strict;
    use warnings;
    use Sys::Hostname;
    use Time::parseDate;

    my ($seconds,$days,$time);
    my $host = hostname;
    my $rup = `rup $host`;

    chomp ($rup);

    ($days,$time) = $rup =~
    m#\S+\s+up\s+(\d+)\s+days?,\s+(\d{1,2}:\d{2}),#;

    print "days is $days\n";
    print "time is $time\n";





    ----------------------

    Thanks,
    JD
     
    , Dec 16, 2004
    #1
    1. Advertising

  2. Re: Regex help - need flexibility to parse with or without blanksin output....

    wrote:
    > Here are 2 possible outputs that rup could give you (if a box has been
    > up for less than a day, there is no "xx days" field):
    >
    > bastion1 up 18:01, load average: 0.17 0.15 0.06
    > bastion2 up 30 days, 22:34, load average: 0.00 0.00 0.02
    >
    > My script works for the 2nd case (the "bastion2" host) - even if "days"
    > is singular. But how do I make my regex more flexible so that it can
    > say:
    >
    > 1. if the box has been up for less than a day, then just skip to the
    > time field and grab it, so that $days=0, $time=18:01 (from the example
    > above).
    >
    > 2. if the box has been up for X days, then I want to grab the number of
    > days so that $days=30, and also grab the time, so that $time=22:34
    > (above).


    <snip>

    > ($days,$time) = $rup =~
    > m#\S+\s+up\s+(\d+)\s+days?,\s+(\d{1,2}:\d{2}),#;
    >
    > print "days is $days\n";


    One way:

    ($days,$time) = $rup =~
    m#\S+\s+up\s+(?:(\d+)\s+days,\s+)?(\d{1,2}:\d{2}),#;
    print "days is " . ($days or 0) . "\n";

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Dec 16, 2004
    #2
    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. me

    Databind() and flexibility?

    me, Feb 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    335
  2. gajo
    Replies:
    7
    Views:
    1,430
    Chris Smith
    Jun 7, 2004
  3. Zombie
    Replies:
    0
    Views:
    363
    Zombie
    Oct 8, 2004
  4. Glen Able

    flexibility of operator new

    Glen Able, Sep 13, 2004, in forum: C++
    Replies:
    4
    Views:
    418
    Tom Widmer
    Sep 14, 2004
  5. Daniel T.
    Replies:
    2
    Views:
    352
    Daniel T.
    Oct 19, 2004
Loading...

Share This Page