help with grep and bash script

Discussion in 'Mac Programming' started by wesg, May 23, 2008.

  1. wesg macrumors regular

    wesg

    Joined:
    Jan 2, 2008
    Location:
    Toronto, ON
    #1
    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?
     
  2. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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.
     
  3. mysticwhiskey macrumors newbie

    Joined:
    Mar 31, 2008
    #3
    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!
     
  4. laprej macrumors regular

    Joined:
    Oct 14, 2005
    Location:
    Troy, NY
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    There's probably a billion ways to do this, but this works too:
    Code:
    uptime | awk -F'(  |,)' '{print $2}'
    -Lee
     
  6. psingh01 macrumors 65816

    Joined:
    Apr 19, 2004
    #6
    As HiRez said, this is how you could do it with cut

    uptime | cut -d',' -f1 | cut -d' ' -f2-5
     
  7. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #7
    There is a command that does this directly it's called "cut"

    Code:
    uptime | cut -c 6-100 - | cut -d , -f 1 -
     
  8. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    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.
     
  9. costabunny macrumors 68020

    costabunny

    Joined:
    May 15, 2008
    Location:
    Weymouth, UK
    #9
    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
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    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
     

Share This Page