Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    17

    Unanswered: Substring replacement

    Hi All,

    I need replace a substring in shell script. The value of newString will be passed in as input argument. However the following script won't take the value of newString. Any suggestion?

    Thanks,

    Mike


    newString=$1
    oldSentence="This is an old way!"

    echo $oldSentence | sed -e 's/old/$(newString)/g'

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool

    Try:

    Code:
    echo $oldSentence | sed -e "s/old/$(newString)/g"
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Aug 2010
    Posts
    17
    It works with double quotes! Thanks!

    How do I replace "\" with "/" using sed?

    Eg. oldstring="\asd\aaa\a.txt"

    I want modify it to "/asd/aaa/a.txt"

    echo oldstring | sed -e "s/\\/\//g" > newstring


    Thanks!

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    You can use any character in the substitute expression so:

    Code:
    sed -e ?\\abc?/abc?
    Not using "/" as the delimiter, makes replacing "/" in the text much easier.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •