Uptime


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Uptime
# 8  
Old 07-27-2008
none of the suggestions work

here is a list of the commands and what output they produce (btw I use Leopard OS X):

uptime | cut -d" " -f4-8 | sed 's/,//g'

-> up 9 mins 2 users



uptime | sed 's/, *[0-9]* users.*$//'

-> 0:18 up 10 mins



uptime | sed 's/.*up \(.*\),.user.*/\1/'
-> 0:19 up 10 mins, 2 users, load averages: 0,61 0,54 0,31



I want it look like that: x days y h z min
I do not want to see the current time, the word up, commas, colons nor the load averages. Furthermore if the machine has only be running for a couple of minutes it should look like that: z min
-> so the output should print nothing else except the minutes (for hours the same: it should not display days yet)
I have been playing around with uptime | awk etc. but this command did not produce the required output since it changes depending on how long the machine has been running for.
Is there a command that solves my problem or is it impossible to produce the required output due to the changing uptime of the computer?
# 9  
Old 07-27-2008
Franklin52's last attempt was very close....
Code:
$ echo "0:19 up 10 mins, 2 users, load averages: 0,61 0,54 0,31" | sed 's/.*up \(.*\),.user.*/\1/'
0:19 up 10 mins, 2 users, load averages: 0,61 0,54 0,31
$ echo "0:19 up 10 mins, 2 users, load averages: 0,61 0,54 0,31" | sed 's/.*up \(.*\),.*user.*/\1/'
10 mins
$

By having that ",.user" only 1 character would be allowed between the comma and the string "user". I changed it to ",.*user" which allows zero or more characters.
# 10  
Old 07-28-2008
@perderabo

the command you used does not work either because you don't use the command uptime you use echo "0:19 up 10 mins, 2 users, load averages: 0,61 0,54 0,31" | sed 's/.*up \(.*\),.user.*/\1/'
and that command will always produce 10 mins as the uptime
and if I change it to
uptime | sed 's/.*up \(.*\),.*user.*/\1/'
the output is the following
-> 6:58

which is my current uptime! (thx so far)

but how do I replace the colon with 'h' and how do I insert a 'min' after the minutes?

And can someone explain this command sed 's/.*up \(.*\),.*user.*/\1/' to me?

Thanks a lot guys for the help! I appreciate it!

update:
my attempt at soving the problem looks like this

uptime | sed 's/.*up \(.*\),.*user.*/\1/' | awk '{sub(":", "h ", $0); sub(" ", "", $0); print "uptime:" $0}'

But I do not know how to insert a 'min' directly after the minutes and how I can get rid of the two white spaces in front of the ' xh' (x for the number of hours).

Last edited by MastaFue; 07-28-2008 at 02:31 AM..
# 11  
Old 07-28-2008
Quote:
Originally Posted by MastaFue
And can someone explain this command sed 's/.*up \(.*\),.*user.*/\1/' to me?
You can save portions of the string with \(.*\) and recall it back with \1, \2, \3 etc.
Here we save that portion after ".*up " and before ",.*user.*" and recall the portion with \1.

Quote:
Originally Posted by MastaFue

But I do not know how to insert a 'min' directly after the minutes and how I can get rid of the two white spaces in front of the ' xh' (x for the number of hours).
Try something like this:
Code:
uptime | sed 's/.*up \(.*\),.*user.*/\1/' | awk -F: '{print "uptime: " $1" h "$2" m"}'

Regards
# 12  
Old 07-28-2008
thank you for the help and the explanation!

@Franklin52

your solution worked but I am not sure if it works if the computer has been on for more than 24 hours because then the string changes because the days are being added. So I will have to wait for a couple more hours to test your solution.

For now I am using this command
uptime | sed 's/.*up \(.*\),.*user.*/\1/' | awk '{sub(":", "h ", $0); sub(" ", "", $0);print "uptime: " $0 "min"}'

again I have to wait until my computer has been on for more than 24 hours to see if everything works fine.

But for now thanks a lot for the help. I will be back in a couple of hours to report whether the commands are working or not.

can someone please test the two commands (if your computer has been on for more than 24hours) I just mentioned in this post and post the outcome in the forum? that would be great because I might habe to restart my Computer so I will have to wait another 24 hours to test.

Update: the two above commands do not work! the first one does not work because the string changes as time advances so one cannot use $1, $2, $3.
the second does not work because it always prints min at the end and if I just started up the computer it prints something like that: x secsmin and x minsmin

So right now I am using this command

uptime | sed 's/.*up \(.*\),.*user.*/\1/' | awk '{sub(":", "h ", $0); sub(" mins", "min", $0); sub(" secs", "sec", $0); print "uptime@mac: " $0}'

It works fine until hours and probably days are displayed. How can I insert "min" after the minutes that are displayed? the "h" works fine because I always replace ":" but I do not know how to solve the problem with the min so it will display it properly at any time, no matter how long my computer has been running for.

Last edited by MastaFue; 07-28-2008 at 05:36 PM.. Reason: Update
# 13  
Old 07-29-2008
alright this command

Code:
uptime | sed 's/.*up \(.*\),.*user.*/\1/' | awk  '{sub(":", " hours ", $0);  sub(" mins", "min", $0); sub(" secs", "sec", $0); sub(",", "", $0); print "uptime: " $0}'

works fine at any time but I am still missing the min when my computer has been on for more than 1hour. Is there a nifty way to insert the word 'min' if the computer has been on for more than one hour and only than. some of the solutions always displayed the 'min' at the end which in turn resulted in outputs like 'xy secsmin' or 'ab minsmin'.

Can anyone help me with my problem, please?

Last edited by MastaFue; 07-29-2008 at 06:52 PM.. Reason: faulty code
# 14  
Old 08-03-2008
alright here is a working solution I found (since I am only answering myself^^) if anybody is interested:

Code:
uptime | sed 's/.*up \(.*\),.*user.*/\1/' | awk  '{sub(":", " hours ", $0);  sub(" mins", " min", $0); sub(" secs", " sec", $0); sub("hrs", "hours", $0); sub(",", "", $0); print "uptime: " $0 " min"}' | awk ' {sub( "min min", "min", $0); sub( "hours min", "hours", $0); sub( "secs min", "sec", $0);  sub( "day min", "day", $0);  sub( "days min", "days", $0); print  $0}'


here is a short explanation: the second awk is there to get rid of unwanted output like secmin, minsmin, hoursmin etc.
so with two awk commands I got my desired output which looks like this

uptime: 3 days 9 hours 29 min
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. War Stories

Once upon an uptime.

Hi All, Having recently started a new job, a Data Center Migration in fact I have been tasked with looking at some of the older Solaris boxes when I came across this little gem. nismas# uname -a SunOS nismas 5.5.1 Generic_103640-27 sun4u sparc SUNW,Ultra-1 nismas# uptime 10:37am up 2900... (2 Replies)
Discussion started by: gull04
2 Replies

2. Shell Programming and Scripting

Extract the uptime from the output of the uptime command

Hi! I want to extract the uptime from the output of the uptime command. The output: 11:53 up 3:02, 2 users, load averages: 0,32 0,34 0,43 I just need the "3:02" part. How can I do this? Dirk (6 Replies)
Discussion started by: Dirk Einecke
6 Replies

3. Linux

uptime options

Hi All is there a way that i can return uptime if the machine has been on for longer than 4 days thanks ab (3 Replies)
Discussion started by: ab52
3 Replies

4. Shell Programming and Scripting

Process Uptime

Hi, I need some help about a script i need to write. I want to check , if some specific process, are running since 2 hours. I tried to use a loop , grep my pid and use find -ctime on /proc directory, to list what i need. for i in `ps -ef |grep process |grep -v grep|awk '{print $2}'`... (2 Replies)
Discussion started by: rokerij
2 Replies

5. AIX

Hmc uptime

HELLO ALL HOW CAN I CHECK HMC UPTIME :confused: :confused: VERSION 6 :D (7 Replies)
Discussion started by: kalaso
7 Replies

6. Solaris

having problem with uptime.

HI All, I have problem with "uptime" on one of the sun server.(SunOS 5.9 Generic_118558-11 sun4u sparc SUNW,Sun-Fire-V240).when i am issuing uptime command its not showing uptime.even its not showing output for who -b. $ uptime 11:01am 1 user, load average: 0.06, 0.04, 0.03 $ who -b $... (3 Replies)
Discussion started by: jeevanbv
3 Replies

7. AIX

HMC uptime

Hey guys!! Was tring to figure out how to find the uptime of an HMC? Any clue Bala (2 Replies)
Discussion started by: balaji_prk
2 Replies

8. UNIX for Dummies Questions & Answers

uptime

On HP-UX, the 13th argument of uptime is sometime the load and sometime the word AVERAGE:??? 14 Jun 06 5:00pm up 44 days, 54 mins, 0 users, load average: 0.00, 0.02, 0.03 14 Jun 06 5:15pm up 44 days, 1:09, 0 users, load average: 0.00, 0.01, 0.01 When the time is in minutes, then the load... (1 Reply)
Discussion started by: qfwfq
1 Replies

9. UNIX for Dummies Questions & Answers

Getting uptime

I'm trying to get the uptime of my computer (Mac OS X) and I can go into the terminal and type "uptime" OK, and that gives me a string with the uptime in it. The problem is that the string changes a lot, and its very difficult to get the data I'm trying to extract out cleanly. Now I have 3... (2 Replies)
Discussion started by: Freefall
2 Replies

10. UNIX for Dummies Questions & Answers

uptime

Hi Folks uptime 12:24pm up 2 days, 3:12, 4 users, load average: 0.00, 0.00, 0.00 what does the load average figure mean.. regards Hrishy (2 Replies)
Discussion started by: xiamin
2 Replies
Login or Register to Ask a Question