Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wesg

macrumors regular
Original poster
Jan 2, 2008
211
0
Toronto, ON
I'm getting into this bash programming thing, but I need some help from the programming gurus out there.

I want to take the output of my uptime script

Code:
20:07  up  1:01, 2 users, load averages: 0.36 0.17 0.14

and only use the text between 20:07 and , 2 users (ie. proper result is up 1:01)

So far I have
Code:
uptime | grep '.*'
. I've used grep with PHP before, but never anything like this. Suggestions?
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
You might be able to use grep, but I'd probably use awk instead and let it use its builtin tokenizing features:

Code:
awk '{print $2,$3}' uptime.txt

(This assumes each line has the same format, I don't know if that's true in your case.)

You could also use the cut utility, or others.
 

mysticwhiskey

macrumors newbie
Mar 31, 2008
25
0
I'm getting into this bash programming thing, but I need some help from the programming gurus out there.

I want to take the output of my uptime script

Code:
20:07  up  1:01, 2 users, load averages: 0.36 0.17 0.14

and only use the text between 20:07 and , 2 users (ie. proper result is up 1:01)

So far I have
Code:
uptime | grep '.*'
. I've used grep with PHP before, but never anything like this. Suggestions?

Hi,

I'm by no means competent with grep, but this seems to work:

Code:
uptime | egrep 'up *[0123456789:]*' -o

Explanation:

egrep = matches a regexp pattern (same as grep -e)
stuff between single quotes = regexp pattern
-o = output only that part of the input that matches the pattern

I'm sure there's a more elegant way to do this though!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
There's probably a billion ways to do this, but this works too:
Code:
uptime | awk -F'(  |,)' '{print $2}'

-Lee
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
There's probably a billion ways to do this, but this works too:
Code:
uptime | awk -F'(  |,)' '{print $2}'

-Lee

As HiRez said, this is how you could do it with cut

uptime | cut -d',' -f1 | cut -d' ' -f2-5
 

Cromulent

macrumors 604
Oct 2, 2006
6,812
1,100
The Land of Hope and Glory
Who ever said Mac OS X was a dumbed down operating system obviously failed to look very far under the surface.

No matter how far I think I'm getting with using the Terminal I always end up finding 100 different ways of completing the same task. Heh, useful info in this thread thanks.
 

costabunny

macrumors 68020
May 15, 2008
2,466
71
Weymouth, UK
this is why I switched back to mac, and am busy converting the unclean...

The beauty of a well thought out GUI, with the power of Unix just a click away - Ahhh bisto...!

and yes there is a plethora of solutions to your script, personally I love using awk with grep. - try that M$!! :p
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
<snip>
try that M$!! :p

While it's not as elegant, I have managed to make my Windows machine at work resemble a real workstation with Cygwin and an X server running under it. I have gcc, awk, grep, vim, etc. all at my disposal locally. I prefer my Mac or a linux system by far, but wanted to point this out for those stuck with Windows in certain scenarios.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.