I am using sed in a shell script to replace a word in a file with a sentence that is stored in a shell variable. The sentence contains spaces and different characters.
Ex:
sentence="new number is > than 2.5 and < than 3.8"
sed "s|word|$sentence|g" -i Example_file.txt
Line in the file:
tile = "word - Final plot"
expected result:
title = "new number is > than 2.5 and < than 3.8 - Final plot"
obtained result:
title = "new - Final plot"
As you can see above, the problem is that sed
always replaces "word" only with the first word of the sentence, it stops at the space. I tried different variations of the command:
sed 's|word|'"$sentence"'|g' -i Example_file.txt
sed "s|word|${sentence}|g" -i Example_file.txt
None of that seems to work. To check if the spaces were indeed the problem I replaced them with "_"
, and when I do that, sed
replaces using the whole sentence.
Anyone knows how can I force sed to take in the spaces and use the whole sentence?