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

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Help with this shell script


    /j
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,395
    Rep Power
    1688
    Your plain text setting is not doing what I think you want it to. Running it by hand:
    Code:
    echo "abcdef 123" | sed 's/./\\\&/g'
    \&\&\&\&\&\&\&\&\&\&
    That is then causing the 'search' part of the sed to have issues.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0
    Do you have any idea how I would fix that?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0
    bump
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    1. Stop bumping threads, it isn't considered polite on this forum

    2. You need to call \\& instead of \&. For instance:
    echo "abcdef 123" | sed 's,\(.\),\\&,g'

    3. With that said, this isn't a good idea to put a backslash in front of every character. I'll leave it to you to figure out why.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,395
    Rep Power
    1688
    Out of interest, what is your definition of 'plain text'?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0
    /j
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,395
    Rep Power
    1688
    But what you are doing is chaging every character to the literal \&. That/those are then used as the search text in the next sed, so your line:
    Code:
    sed "s/$plain/$2/g" "$3" > "$tmpfile"	# Edit
    Would actually become (once the $plain is evaluated):
    Code:
    sed "s/\&\&\&\&\&\&\&\&\&\&/$2/g" "$3" > "$tmpfile"	# Edit
    What I think you want is a search string like "abcdef 123" being converted to "\a\b\c\d\e\f\ \1\2\3" - which in itself will potentially cause issues as \1, \2, etc., have special meaning in sed.

    Why do you need to do such conversion on the search text and not the substitution text?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0
    I'm not sure. I'm trying to think of another way to write it. How would you write it?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,395
    Rep Power
    1688
    How I would write it would depend on what was needed ... hence the 'what do you mean by plain text?' questions!
    I am beginning to suspect I know what you mean and that is the original file does, or may, contain control characters which you wish to replace/remove in a (semi-)controlled fashion. If that is the case then work backwards ... from the command line (and working with a copy of the file!!!) work out what you need to identify the 'errant' strings, thus being able to replace them. Once you know what you need to supply sed you can take the next step back to find out how to supply those characters to sed, and then to how to get the user to input them.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo