1

80I am trying to read a file and replace a placeholder with the content of another file. The problem is the variable contains urls which seems to cause problems in sed. In addition: what to do to keep the new lines from images.txt? Is there a way to make my solution work or is there maybe another solution that is better suited for my problem? I want to overwrite content of a file with the content of a backup file. In addition the step should include replacing a placeholder with the content of a third file. Thank you. What I currently use:

<images.html
TEXT=$(<images.txt)
sed 's~URLS~$TEXT~g' imagesbu.html > images.html

This does not work and just shows:

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

Content of the file is:

https://cdn.tutsplus.com/vector/uploads/legacy/tuts/165_Shiny_Dice/27.jpg
https://cdn.tutsplus.com/vector/uploads/legacy/tuts/165_Shiny_Dice/27.jpg

IF there is no newline in the file it works.

user2196234
  • 71
  • 1
  • 1
  • 8
  • possible duplicate of [Replace a word with multiple lines using sed?](http://stackoverflow.com/questions/10107459/replace-a-word-with-multiple-lines-using-sed) – devnull Mar 14 '14 at 13:43
  • Refer to the linked question for an answer. – devnull Mar 14 '14 at 13:43

1 Answers1

5

Try altering your sed delimiter so that it is not a forward slash:

sed "s~URLS~$TEXT~g" imagesbu.html > images.html

Edit: Your original sed command doesn't work because of the above, and because you are trying to replace a single word with multiple lines. Try awk instead:

awk -v u="$TEXT" '{gsub(/URLS/,u)}1' imagesbu.html > images.html
Josh Jolly
  • 11,258
  • 2
  • 39
  • 55
  • Same error but a char 91. I guess I need to escape all special characters in $TEXT. Could this be the problem? – user2196234 Mar 14 '14 at 13:17
  • @user2196234 This is because you were using `/` as delimiter, while it is also in the `$TEXT`. Look for another one that is not in the variable, as it looks like `~` is also in the var. – fedorqui Mar 14 '14 at 13:22
  • The sed command is failing because of the newlines contained in images.txt - are you trying to replace the placeholder with everything inside the file? – Josh Jolly Mar 14 '14 at 13:37
  • Yes I do. I would also like to keep the newlines to use them as delimiters later (if possible). – user2196234 Mar 14 '14 at 13:41
  • Is the placeholder on its own line within `imagesbu.html`, or is it contained within some other text? – Josh Jolly Mar 14 '14 at 13:44
  • It is contained in other text. ...="URLS"; – user2196234 Mar 14 '14 at 13:48