Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex help - need flexibility to parse with or without blanks in output....

Reply
Thread Tools

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

 
 
tudmuf2b@onebox.com
Guest
Posts: n/a
 
      12-16-2004
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:arseDate;

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

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-16-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Toshiba Corporation today reconfirmed the flexibility and expandability of the HD DVD format with the announcement Allan DVD Video 0 09-25-2005 02:04 AM
ultimate flexibility in schemas Zombie XML 0 10-08-2004 05:28 PM
flexibility of operator new Glen Able C++ 4 09-14-2004 03:19 PM
Anonymous blocks for memory saving and flexibility? gajo Java 7 06-07-2004 05:07 PM
Databind() and flexibility? me ASP .Net 2 03-03-2004 11:05 AM



Advertisments