1

I have done several searches on this and while the solutions seem obvious, I can't seem to get a definite one for my particular situation. I have a file that contains a string "${string}" and I would like to replace it with another string that happens to be a file path "/tmp/myfilepath".

"sed" apparently seems to be the most popular choice to do this in linux. I am using the following command:

sed -i 's/"${string}"/"/tmp/myfilepath"/g' myFile.txt

I get the error message...

sed: -e expression #1, char 5: unknown option to `s'

I am using the double quotes because it has additional slashes in it. I have tried mix-matching single and double quotes, but nothing seems to work. Any thoughts or solutions? Thanks in advance.

JohnnyB
  • 13
  • 1
  • 4
  • possible duplicate of [Escape a string for sed search pattern](http://stackoverflow.com/questions/407523/escape-a-string-for-sed-search-pattern) – Karoly Horvath Oct 31 '11 at 20:50

2 Answers2

4

You should escape the backslashes in your substitution string:

sed -i 's/"${string}"/"\/tmp\/myfilepath"/g' myFile.txt

Or use a different delimiter with the substitution command, such as a colon:

sed -i 's:"${string}":"/tmp/myfilepath":g' myFile.txt
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
1

Use a different delimiter:

sed -i "s|${string}|/tmp/myfilepath|g" myFile.txt

If the string can contain special characters, fix it first.

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176