in reply to Parsing 'uptime' output

Here's one way:

my @uptime = split " ", qx(/usr/bin/uptime); my %uptime; @uptime{'Time', 'Uptime', 'Current Users', 'CPU Load'} = ( $uptime[0], join ' ', @uptime[1..4], join ' ', @uptime[5..6], join ' ', @uptime[7..$#uptime] );

Update: Repaired typo, ++sauoq for the eyes. There seems to be a problem with the parsing...fixed, not pretty, but neither is uptime :-\

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Uptime
by sauoq (Abbot) on Aug 18, 2002 at 22:03 UTC
    That won't pull out the time or uptime correctly either. The time is first, separated from the uptime by space. Then the number of days and hours:minutes of uptime is separated by a comma. Zaxo's should work now that he fixed it.

    Just matching might be better than trying to use split.

    @uptime{'Time', 'Uptime', 'Users', 'Load'} = qx(/usr/bin/uptime) =~ /^\s*(\S*)\s*([^,]*,[^,]*),([^,]*),(.*)/;
    Update: Tested and fixed now.
    Update 2: sauoq sighs. Thanks guha. Now it's really fixed.
    -sauoq
    "My two cents aren't worth a dime.";