Showing only the uptime from uptime [unix]

Rich Bradshaw picture Rich Bradshaw · Nov 15, 2009 · Viewed 11.5k times · Source

I'd like to trim the output from uptime

20:10  up 23 days,  3:28, 3 users, load averages: 3.84 1.06 0.64

so that it just shows:

23 days

I've tried using sed, but I'm not sure it's the right tool for the job, and don't have much experience using it.

How can I achieve the output I want?

Answer

Consider using cut.

  uptime | tr "," " " | cut -f6-8 -d" "

seems to work on my MacBook. Here I've also used tr to kill an extraneous ",". There is a bit of an issue with different formats for short and long uptimes.


A possible sed solution:

uptime | sed 's/.*up \([^,]*\), .*/\1/'

which doesn't rely on the string "days" appearing in the output of uptime.