This should get you going - I'm not sure what happens when uptime > 365 days if it converts to years too. I'll have to login into one of my UN*X boxes to see =)
#!/usr/bin/perl -w use strict; # test on a string with just hours my $uptime = ' 9:12pm up 2:13, 5 users, load average: 0.84, 0.62, + 1.03'; if( $uptime =~ /up\s+((\d+) days,\s+)?(\S+),/ ) { my ($dayup,$timeup) = ( $2,$3); # $1 is the first enclosing parens + which we don't want print "timeup is ", defined $dayup ? " $dayup days and " : '', "$t +imeup hours\n"; } # test again on a string with days $uptime = "6:37PM up 4 days, 2:05, 2 users, load averages: 1.99, 1.65, + 1.47"; if( $uptime =~ /up\s+((\d+) days,\s+)?(\S+),/ ) { my ($dayup,$timeup) = ( $2,$3); # $1 is the first enclosing parens + which we don't want print "timeup is ", defined $dayup ? " $dayup days and " : '', "$t +imeup hours\n"; }
Another way to attack this if you aren't comfortable with those regexps is to strip away the things you know you don't need and just get what you know you want ... Everything between 'up' and 'XX users'.
my $uptime = ' 9:12pm up 2:13, 5 users, load average: 0.84, 0.62, + 1.03'; my ($keep) = ( $uptime =~ /up\s+(.+),\s+\d+\s+users/);
HTH.

In reply to Re: pattern matching by stajich
in thread pattern matching by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.