Note: The following question is related to this post, but both the focus of the question and the format of the input variable are different (i.e., contents of a multi-line file). Thus, the correct solution is likely different as well.
It appears that replacing a keyword in a string with a multi-line variable via sed
works as long as said variable contains explicit newline characters (\n
).
$ target="foo \n bar \n baz"
$ echo "qux\nquux" > file
$ solution=$(cat file)
$ echo "$solution"
qux\nquux
$ echo $target | sed -e "s|bar|$solution|"
foo \n qux
quux \n baz
However, when I open the file file
in a text editor and substitute the newline character with a linebreak, the replacement with sed fails.
# Newline character was manually replaced with linebreak in texteditor.
$ solution=$(cat file)
$ echo "$solution"
qux
quux
$ echo $target | sed -e "s|bar|$solution|"
sed: -e expression #1, char 9: unterminated `s' command
How do I have to change the sed-command to conduct the sought substitution when the input variable that does not have explicit newline characters?