#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    How to grep from a list


    Hi

    Below is what I am trying to do. I have a variable which contains a list of commands seperated by ":". While trying to use a for loop to get each command, it doesnt retrun the correct results

    Example:

    var="command -1:command -2*: command something*"
    for c in `echo $var | tr ':' ' '`
    do
    echo $c
    done

    Instead of getting the below
    command -1
    command -2*
    command something

    I get
    command
    -1
    command
    -2*
    command
    something

    Please help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Is this supposed to be Perl?

    It looks like shell to me. But some parts are looking a bit like Perl. Don't do such things. Do it in Perl or in shell script, not a mixture of both (even though you may call some shell command from Perl if you need).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    This is shell and I keep posting this in Perl coz I dont see a seperate section for shell scripting. I am not sure if I missed it but I did look for it
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Try it using an array:
    Code:
    $ cat test.sh
    var=("command -1:" "command -2*:" "command something*")
    for c in "${var[@]}"
    do
      echo $c | sed 's/\://g'
    done
    
    $ test.sh
    command -1
    command -2*
    command something*
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    Thanks a bunch!!!!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Originally Posted by Elizabeth H
    This is shell and I keep posting this in Perl coz I dont see a seperate section for shell scripting. I am not sure if I missed it but I did look for it
    Allright, sorry, but then, may be you should tell it upfront, because I was initially thinking: "oh, man, what a mess, she really understands nothing to Perl and thinks it is like shell." ;-)

    I guess you have a solution by now.
  12. #7
  13. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    Unix Help or Linux Help is probably where it should be posted, but the description doesn't mention shell scripting

    Windows Help used to have a good few WSH questions at one time, but I don't spend too much time there
    --Ax
    without exception, there is no rule ...
    Handmade Irish Jewellery
    Targeted Advertising Cookie Optout (TACO) extension for Firefox
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


    09 F9 11 02
    9D 74 E3 5B
    D8 41 56 C5
    63 56 88 C0
    Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
    -- Jamie Zawinski
    Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
    BIT COINS ANYONE
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    I'm on Solaris and it is saying there is no -d option for grep.

    I see what you were doing there. Nice and simple if I were on something a bit more full featured.

    so close, so far away...

    Thanks
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Maybe you have egrep installed.

IMN logo majestic logo threadwatch logo seochat tools logo