Issue
I'm trying to replace a bloc in a file (file1) from another one coming from a second file (file2). File2 contains multiline, let's say:
Print 1
Print 2
file2="$(cat file2)"
sed -i "/Do_something/,/^}/c\
echo ${file2} " ${file1}
The good thing is that it works but it replaces it in a single line. How can I replace with multiline? I tried to put directly the variable but It doesn't work, it works only with 'echo'. Also if I add double quotes, it's doesn"t work.
Solution
You don't need to read file2
in a shell variable. Just get it done in sed
using r file
command:
sed -e '/Do_something/,/^}/{/^}/!d; r file2' -e 'd;}' "$file1"
Answered By - anubhava Answer Checked By - Katrina (WPSolving Volunteer)