Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top