mattman has asked for the wisdom of the Perl Monks concerning the following question:

I would like to include the output of uptime on my web page as an SSI, but I can't figure out how to parse it so only the time and not the processor info appears. Here's a sample output:
  4:06pm  up 14 days, 14:39,  2 users,  load average: 1.97, 1.62, 1.54
and I'd like it to just print the "14 days, 14:39" part. Because the spacing is generated dynamically, and I don't know enough C to change the code to create my own binary, anybody have any ideas on doing it in Perl? Thanks

Matt

Replies are listed 'Best First'.
RE: parsing output of uptime
by vroom (His Eminence) on Apr 21, 2000 at 00:25 UTC
Re: parsing output of uptime
by btrott (Parson) on Apr 21, 2000 at 00:19 UTC
    This will work. There may be a better/faster way.
    my $uptime = `uptime`; if ($uptime =~ /^\s+\S+\s+up\s+(\S+\s+\S+,\s+\S+),/) { print $1; } else { print "uptime gave me a weird format!"; }
    Another way of doing it would be to split the output on \s+, then collect the parts that you want.
Re: parsing output of uptime
by dsdisc (Initiate) on Apr 21, 2000 at 00:21 UTC
    I just tried this and it seemed to do a good job of splitting the output:
    perl -e '$u = `uptime`; print join("\n",(split(/\s{2,}/,$u)));'

    which gave me:
    1:19pm
    up 3 days,
    4:36,
    2 users,
    load average: 0.22, 0.31, 0.36

    so if you had:
    @uptime = split(/\s{2,}/,`uptime`);

    The part you want should be $uptime1 and $uptime2
RE: parsing output of uptime
by Anonymous Monk on Apr 21, 2000 at 00:24 UTC

    Sorry, didn't realize this place submitted comments in HTML. I'll try again:

    $_ = '4:06pm up 14 days, 14:39, 2 users, load average: 1.97, 1.62, 1.54'; my ($days, $hours, $minutes) = /(\d+) +days, +(\d+):(\d+)/;

    Should work, haven't tested it.

Re: parsing output of uptime
by subpop (Sexton) on Apr 21, 2000 at 04:54 UTC
    you could always parse the output of /proc/uptime and make your own calculations for how long it has been up.
RE: parsing output of uptime
by Anonymous Monk on Apr 21, 2000 at 04:47 UTC
    perl carries rather a lot of overhead for a SSI. There is more than one way to do it. How about: uptime | awk '{print $3, $4, $5}' | tr ',' ' '
Re: parsing output of uptime
by btrott (Parson) on Apr 21, 2000 at 10:58 UTC
    One more thing on this: take a look at my newly-posted tutorial, Tie: Creating Special Objects, as it contains a regular expression to parse out the interesting parts of the uptime output. (It also contains a bunch of other neat stuff using tie and object oriented Perl, so give that a look, too.)
RE: parsing output of uptime
by Anonymous Monk on Apr 21, 2000 at 15:44 UTC
    #!/usr/bin/perl $uptime = `/usr/bin/uptime`; @tmp = split(); # Split $uptime up by whitespace $result = "$uptime2 $uptime3 $uptime4"; print "$result\n";
Re: parsing output of uptime
by Maqs (Deacon) on Apr 22, 2000 at 15:10 UTC
    $uptime=`uptime`; $uptime =~ s/.*(\d+:\d+).*/$1/;