CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
regexp for uptime

 



charlesD
Novice

Apr 12, 2009, 4:48 PM

Post #1 of 7 (3602 views)
regexp for uptime Can't Post

Hi,
I'm totally new to Perl, and I need to get the following:
Assuming the output of /usr/bin/uptime is:
17:22 up 2 days, 1 min, 2 users, load averages: 1.65 0.62 0.37

I need a regexp that will get:
2 days, 1 min

Without the last comma, please.
I've searched and Googled all afternoon, I have a copy of O'Reilly Learning Perl, but I can't make heads or tails of it. I'm just too new. I need some examples that match what I'm trying to do so I can learn.


--
Thanks,
Charles

Using Perl since 4/12/09
perl 5.8.9, FreeBSD 6.4-STABLE
http://bubbabbq.homeunix.net


KevinR
Veteran


Apr 12, 2009, 8:14 PM

Post #2 of 7 (3599 views)
Re: [charlesD] regexp for uptime [In reply to] Can't Post

One way:


Code
my $string = '17:22 up 2 days, 1 min, 2 users, load averages: 1.65 0.62 0.37'; 
my ($days_mins) = $string =~ /(\d+ days,\s*\d+ min)/;
print $days_mins;


Its always good to see what you have tried, even if it does not work. You may have been very close or you may not have, but posting the code you have tried can give an indication of your progress.
-------------------------------------------------


charlesD
Novice

Apr 12, 2009, 10:32 PM

Post #3 of 7 (3594 views)
Re: [KevinR] regexp for uptime [In reply to] Can't Post

Thanks Kevin,
Of course, your code works correctly.
Problem is, when I modify it slightly and stick it in my script I get errors.

#!/usr/bin/perl -w
print "Content-type: text/plain","\n\n";
$uptime = `/usr/bin/uptime`;
$sysdate = `date "+%A, %B %d %Y"`;
$systime = `date +"%H:%M"`;
($days_mins) = $uptime =~ /(\d+ days,\s*\d+ min)/;
($load_average) = ($uptime =~ /averages: ([^ ]{4})/);
print "The current system date is: ", $sysdate;
print "The current system time is: ", $systime;
print "The system has been up for: ", $days_mins;
print "The current load average on the system is: ", $load_average, "\n";
exit (0);

larry:bin charles$ ./up.pl
Content-type: text/plain

The current system date is: Monday, April 13 2009
The current system time is: 00:30
Use of uninitialized value in print at ./up.pl line 10.
The system has been up for: The current load average on this machine is: 0.23


--
Thanks,
Charles

Using Perl since 4/12/09
perl 5.8.9, FreeBSD 6.4-STABLE
http://bubbabbq.homeunix.net


KevinR
Veteran


Apr 12, 2009, 11:17 PM

Post #4 of 7 (3587 views)
Re: [charlesD] regexp for uptime [In reply to] Can't Post

I don't know what the possible return values of the uptime application are, I only went by your sample output:


Code
17:22 up 2 days, 1 min, 2 users, load averages: 1.65 0.62 0.37


There is a good chance that could be different depending on the amount of uptime and the regexp will not match if that is the case. For example, what if the uptime where only 3 hours and X minutes, it might look like this:


Code
17:22 up 3 hours, 6 min,  2 users, load averages: 1.65 0.62 0.37


In which case there is no match. You might want to look at the Unix::Uptime module:

http://search.cpan.org/~pioto/Unix-Uptime-0.3201/lib/Unix/Uptime.pm

I have never used it but it looks like it may be what you need or can use.
-------------------------------------------------


charlesD
Novice

Apr 13, 2009, 5:47 AM

Post #5 of 7 (3580 views)
Re: [KevinR] regexp for uptime [In reply to] Can't Post

Using the code you sent, I can change the values in the string to test it.

I'm curious about the failure in my script.
Use of uninitialized value in print at ./up.pl line 10.

What exactly does that mean?


--
Thanks,
Charles

Using Perl since 4/12/09
perl 5.8.9, FreeBSD 6.4-STABLE
http://bubbabbq.homeunix.net


KevinR
Veteran


Apr 13, 2009, 9:35 AM

Post #6 of 7 (3570 views)
Re: [charlesD] regexp for uptime [In reply to] Can't Post

It means one of the variables you are printing has no value, here is a very simplified example:


Code
use warnings; 
$foo = $1;
print $foo;


$foo is assigned the value of $1, but since $1 has no value $foo also has no value (uninitialized) and when you try and print an uninitialzed variable the warnings pragma (or the warnings switch on the shebang line) returns the warning you are getting. Basically its letting you know there is something you should check to make sure it will not cause a bigger problem somewhere down the line. You can avoid the warnings by assigning default values:


Code
use warnings; 
$foo = $1 || 'foo';
print $foo;


Thats just one example of how it could be done.
-------------------------------------------------


charlesD
Novice

Apr 13, 2009, 9:57 AM

Post #7 of 7 (3567 views)
Re: [KevinR] regexp for uptime [In reply to] Can't Post


In Reply To
It means one of the variables you are printing has no value, here is a very simplified example:


Code
use warnings; 
$foo = $1;
print $foo;


$foo is assigned the value of $1, but since $1 has no value $foo also has no value (uninitialized) and when you try and print an uninitialzed variable the warnings pragma (or the warnings switch on the shebang line) returns the warning you are getting. Basically its letting you know there is something you should check to make sure it will not cause a bigger problem somewhere down the line. You can avoid the warnings by assigning default values:


Code
use warnings; 
$foo = $1 || 'foo';
print $foo;


Thats just one example of how it could be done.


OK, I understand. So much to learn. :-)

I have actually completed my script that this is all releated to, using whatever workarounds I could find.

#!/usr/bin/perl -w
print "Content-type: text/plain","\n\n";
$uptime = `/usr/bin/uptime`;
$sysdate = `date "+%A, %B %d %Y"`;
$systime = `date +"%H:%M"`;
($load_average) = $uptime =~ /averages: ([^ ]{4})/;
$ps = `ps ax | wc -l | sed 's/^[ \t]*//'`;
print "The current system date is: ", $sysdate;
print "The current system time is: ", $systime;
open(UPTXT, "up.txt") || die "Cannot Open: $!";
while (<UPTXT>) {
chomp;
print "The system was last booted ", $_, "\n";
}
print "The current number of running processes is ", $ps;
print "The current load average on the system is: ", $load_average, "\n";
exit (0);


Ther is a cron job that run when the server is booted that does "echo `date` > up.txt" which is the file I read as UPTXT.
You can view the actual web page here:
http://bubbabbq.homeunix.net/up_cgi.shtml


--
Thanks,
Charles

Using Perl since 4/12/09
perl 5.8.9, FreeBSD 6.4-STABLE
http://bubbabbq.homeunix.net

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives