How to modify the uptime command on shell?

787 Views Asked by At

When I type in uptime in terminal it gives me something like this

 16:44:17 up  7:50,  load average: 0.31, 0.47, 0.54

I want to make it to be something like this

Uptime: 7h 50m
Load average: 0.31, 0.47, 0.54

and save it to uptime.txt

How can I do that? I'm sorry for the dumb question, I'm still new in this and I've googled before but I find nothing useful.

2

There are 2 best solutions below

1
On BEST ANSWER

this ugly command will work:

uptime | sed 's#[0-9][0-9]:[0-9][0-9]:[0-9][0-9]##g' | sed 's#:#h #g' | sed -E 's#, [^,]+users,#m#g' | sed 's/ days,/d/g'|sed 's/  up/Uptime:/g' | sed 's/load averageh/\nLoad average:/g'
0
On

You can use awk with uptime:

uptime | awk -F ' up | load average:' '{sub(/,.*$/, "", $2); 
             printf "Uptime: %s\nLoad average: %s\n", $2, $NF}'
Uptime: 23 days 16:14
Load average:  2.19, 1.91, 1.88