Extract the uptime from the output of the uptime command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract the uptime from the output of the uptime command
# 1  
Old 04-14-2011
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:
Code:
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

Last edited by Franklin52; 04-14-2011 at 07:08 AM.. Reason: Please use code tags, thank you
# 2  
Old 04-14-2011
Code:
echo $(uptime) | sed 's/^.\+up\ \+\([^,]*\).*/\1/g'


Last edited by |UVI|; 04-14-2011 at 07:10 AM.. Reason: Please use code tags, thank you
# 3  
Old 04-14-2011
Code:
uptime | awk '{print $3}'

# 4  
Old 04-14-2011
Quote:
Originally Posted by Franklin52
Code:
uptime | awk {print $3}'

I think this works only if uptime < 1 day Smilie
# 5  
Old 04-14-2011
Quote:
Originally Posted by |UVI|
I think this works only if uptime < 1 day Smilie
Right, thanks, it should be something like:
Code:
uptime | awk -F, '{sub(".*up ",x,$1);print $1}'

# 6  
Old 04-14-2011
Here's a couple more "uptime" examples to play with.
They are UK number format (unlike the O/P who has a comma as a decimal separator).
Note that the format increases the number of comma-separated fields and also changes if the uptime is an exact number of hours.
Code:
 12:55pm  up 105 days, 21 hrs,  2 users,  load average: 0.26, 0.26, 0.26
  1:41pm  up 105 days, 21:46,  2 users,  load average: 0.28, 0.28, 0.27

It can be done by converting commas to newlines and ignoring everything from the line containing "users". My trial script got too convoluted to post!
# 7  
Old 05-12-2011
Code:
>> uptime | awk -F, '{sub(".*up ",x,$1);print $1}'

Exp-
Code:
# uptime
11:29:16 up 47 days, 20:34,  2 users,  load average: 0.01, 0.01, 0.00

# uptime | awk -F, '{sub(".*up ",x,$1);print $1}'
47 days

It lists only days but there is no output for hours 20:34. So there is little modification:
Code:
# uptime | awk -F, '{sub(".*up ",x,$1);print $1,$2}'
47 days  20:34


Last edited by Franklin52; 05-12-2011 at 04:07 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

uptime command output when the server is running more than one year?

What is the uptime command output when the server is running more than one year? My doubt is whether it show in number of days format or number years and number of days format? For example, Assume the server is running 400 days 3 hrs 3 min 3 secs. The output like 400 days 3:3 min or 1 year 5... (3 Replies)
Discussion started by: maruthu
3 Replies

2. Solaris

uptime command not showing how long the system has been up

Hello folks, uptime command not shows how long the system has been up. I know it come from a corruption of /var/adm/utmpx file. I've done : cat /dev/null > /var/adm/utmpx Now who and last commands work fine. But uptime still give me back an answer without the "up time". In which... (6 Replies)
Discussion started by: gogol_bordello
6 Replies

3. Solaris

uptime and who giving improper or no output

Hello Everyone, One of our servers is showing a strange issue, let me paste the output root # uptime 4:37pm 3 users, load average: 0.00, 0.04, 0.04 Its been running since months but you can see after time there isn't any output like up 191 days(s). Even the who command with b... (1 Reply)
Discussion started by: vishalaswani
1 Replies

4. Solaris

uptime and last reboot command in solaris

Hi, Can someone explain in detail what 'uptime' ,'last reboot' and 'who -b' commands do in solaris. this commands are not executing in every solaris box. why this is happening. Has solaris got some inbuilt commands into it. If yes then where i have found them? Thanks,Soubhik (6 Replies)
Discussion started by: soubmukh
6 Replies

5. AIX

reset the counter days for uptime command

hello, i send the uptime command in the AIX and the days that is UP 14652 days this is around 40 years, today is with the correct date&time, hos can I reset the counter days? somebody can help me? regards (3 Replies)
Discussion started by: timflr
3 Replies

6. Shell Programming and Scripting

Extracting load average from uptime command

The output ofthe uptime command gives: 9:40am up 9 days, 10:36, 4 users, load average: 0.02, 0.01, 0.00 How can i extract the portion "load average: 0.02, 0.01, 0.00". (3 Replies)
Discussion started by: proactiveaditya
3 Replies

7. AIX

After run ps , uptime , w command I get reply "killed"

Hi, After run ps , uptime , w command I get reply "killed" as normal dba and staff group user. As root every command works fine. I cheched all the user settings , right with other servers and I could not find any error and other settings. The oslevel is 5300-10-01-0921. Any idea to... (12 Replies)
Discussion started by: boki
12 Replies

8. Shell Programming and Scripting

get only the up time from uptime command

Hi all,:o i am new to shell scripting and i have aproblem like i just want to extractthe uptime of the system from an uptime command which gives the output as the Current time , how long the system has been running,how many users are surrently logged on and the system load averages for past 1,5,... (5 Replies)
Discussion started by: tulip
5 Replies

9. UNIX for Dummies Questions & Answers

Interpretation of the uptime command

Hi there, do someone have detailed information how to interpret the uptime command or rather which values can be called normal? (i know what the information means, but i have no idea if these values are ok or to high: 3:02pm an 13:53, 2 Benutzer, Durchschnittslast: 10,06, 12,05, 13,00) ... (5 Replies)
Discussion started by: odin1999
5 Replies

10. 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
Login or Register to Ask a Question