12

Uptime

Posted in System by matt 2714 days ago

This Geeklet tells you how long your Mac has been on for (e.g. Up 3 days, 22 hours and 28 minutes).

It is quite easy to rearrange the output format to what you want. The command is:

uptime | sed -e "s/:/ /2" | sed -e "s/,/ /2" | awk '{print "Up " $3 " days, " $5 " hours and " $6 " minutes"}'

As you can see, $3 will print the number of days, $5 the number of hours, and $6 the number of minutes. You can rearrange these, for example:

uptime | sed -e "s/:/ /2" | sed -e "s/,/ /2" | awk '{print "UPTIME: " $3 " days, " $5 ":" $6}'

will give UPTIME: 3 days, 22:28 instead.

Screenshot

Geeklet files to download

Comments

User Avatar
ceph 2708 days ago
Both break if you have less than 1 day uptime.
User Avatar
matt 2684 days ago
Ah yes, of course. Anyone have any suggestions for a better way to format the output of the uptime command?
User Avatar
NimbusSoftware 2649 days ago
Here's a modification of that should work for you

#Uptime

uptime | awk '{sub(/[0-9]|user,|users,|load/, "", $6); sub(/mins,|min,/, " minutes", $6); sub(",", " minutes", $5); sub(":", " hours ", $5); sub(/hrs,|hr,/, " hours", $4); sub("h ", " hours ", $4); sub(/days,/, " days ", $4); sub(":", " hours ", $3); sub(",", " minutes", $3); print "nUp " $3 $4 $5 $6}' | sed -e 's/ 1 days/ 1 day/g' -e 's/ 1 hours/ 1 hour/g' -e 's/1 minutes/1 minute/g' -e 's/usermin//g'
User Avatar
boissonnfive 2424 days ago
Sorry NimbusSoftware, it doesn't work perfectly:

$ uptime

12:35 up 4:20, 2 users, load averages: 2,20 2,29 2,21

$ uptime | awk '{sub(/[0-9]|user,|users,|load/, "", $6); sub(/mins,|min,/, " minutes", $6); sub(",", " minutes", $5); sub(":", " hours ", $5); sub(/hrs,|hr,/, " hours", $4); sub("h ", " hours ", $4); sub(/days,/, " days ", $4); sub(":", " hours ", $3); sub(",", " minutes", $3); print "nUp " $3 $4 $5 $6}' | sed -e 's/ 1 days/ 1 day/g' -e 's/ 1 hours/ 1 hour/g' -e 's/1 minutes/1 minute/g' -e 's/usermin//g'

nUp 4 hours 20 minutes2users minutes

Log in to comment or register here.