GeekTool


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers GeekTool
# 1  
Old 04-26-2008
GeekTool

Hello to whoever is reading this!

I try to display my uptime on my desktop with a program called GeekTool.
For that purpose I use this command

uptime | awk '{print "Uptime : " $3 " " }'

So it looks like this

Uptime : 3:01,


My first question is how to remove the comma behind the uptime. And my second question how can I add an 'h' behind the hour digit and a 'min' behind the minutes.

Thank you for your help in advance!
# 2  
Old 04-26-2008
# uptime | sed -e 's/:/h /g' -e 's/,/min/g' | awk '{print "Uptime: " $5 " "$6}'
# 3  
Old 04-26-2008
thank you for the quick response

when i use the line

uptime | sed -e 's/:/h /g' -e 's/,/min/g' | awk '{print "Uptime: " $5 " "$6}'


it looks like that

Uptime: 45min 2

But I expected it to look like this

Uptime: Xh Ymin

and please could you give me a short explanation as to what you did exactly in the line?
# 4  
Old 04-26-2008
Might as well use awk all the way.

Code:
uptime | awk '{sub(":", "h", $3); sub(",", "min", $3); print "Uptime: " $3}'

The sub function might not exist in your awk, but there are other similar functions you can explore.
# 5  
Old 04-26-2008
Quote:
Originally Posted by MastaFue
uptime | sed -e 's/:/h /g' -e 's/,/min/g' | awk '{print "Uptime: " $5 " "$6}'


it looks like that

Uptime: 45min 2
Probably what happened was that there was another colon earlier on the line. The sed script replaces ":" with "h " (with a space after it) which of course affects what's in $3 $4 $5 etc when it reaches awk. The all-awk solution I posted above should not have this problem.

s/a/b/g means replace a with b everywhere on every input line. (Without the /g it only changes the first occurrence.)
# 6  
Old 04-26-2008
Thank you era, you solved my problem.

I am new at unix so I have to ask some questions if you don't mind.

How did you exactly replace the comma in your command line and what does sub actually do?

Last but not least, is there a way of not showing the zero in front of the minutes in this example

Uptime: 4hrs 08min

?
# 7  
Old 04-26-2008
sub does a substitution (replacement); it's basically equivalent to the sed command we discussed earlier, except you can also name an awk variable you want to perform the change on (in my case that was $3; if you leave it out, it modifies $0, i.e. the whole input line). So sub("foo", "bar", $3) will replace foo in $3 with bar. (Just the first occurrence, if there are multiple.)

Replace ":0" with just ":" before replacing ":" with "h " to get rid of any leading zero on the minutes field. (If the substitution can't be performed, it does nothing, so it's harmless to attempt it even on lines which don't have a zero.)

Last edited by era; 04-26-2008 at 04:07 PM.. Reason: Note that failed substitution is harmless
 
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

geekTool

Hello! Here is my problem: I try to display the uptime of my computer on my desktop with the help of a tool called GeekTool (for Mac). I want the uptime output to look like that: x day(s) y h z min (but if the pc is only running e.g for several minutes it should leave out the day and the... (1 Reply)
Discussion started by: MastaFue
1 Replies
Login or Register to Ask a Question