Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing only the uptime from uptime [unix]

Tags:

regex

sed

uptime

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?

like image 531
Rich Bradshaw Avatar asked Nov 27 '22 00:11

Rich Bradshaw


2 Answers

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.

like image 141
dmckee --- ex-moderator kitten Avatar answered Dec 28 '22 14:12

dmckee --- ex-moderator kitten


uptime | sed -E 's/^.+([0-9]+ days).+$/\1/'
like image 23
jtbandes Avatar answered Dec 28 '22 15:12

jtbandes