Issue
I have a problem that is very similar to this SO thread: how to replace all lines between two points and subtitute it with some text in sed
Consider a problem like this:
$ cat file
BEGIN
hello
world
how
are
you
END
$ sed -e '/BEGIN/,/END/c\BEGIN\nfine, thanks\nEND' file
BEGIN
fine, thanks
END
How might I inject text that I have saved to a variable like:
str1=$(echo "This is a test")
How can I inject str1's output in the place of fine, thanks
, like:
sed -e '/BEGIN/,/END/c\BEGIN\n$str1\nEND' file # fails to work as hoped
I also want to save the output to overwrite the file.
Solution
You can probably use this sed
:
sed -e '/BEGIN/,/END/ {//!d; /BEGIN/r file.html' -e '}' file
This will insert content of file.html
file between BEGIN
and END
. To save changes back to file:
sed -i -e '/BEGIN/,/END/ {//!d; /BEGIN/r file.html' -e '}' file
Answered By - anubhava