I need to write a simple script to replace a block of text in a configuration file with the contents of another file.
Let\'s assume with have the following simplifie
I was unable to get Dennis solution easily working on OS X (its BSD sed is slightly different). I found this other solution that I was able to make work on both Linux and OS X (I have a mixed environment). The original version on superuser.com works only on Linux, here I fixed it:
lead='^$'
tail='^'
sed -e '/'"$lead"'/,/'"$tail"'/{ /'"$lead"'/{p; r realm.xml' -e' }; /'"$tail"'/p; d;} ' server.xml
Here a version of Dennis code that works also on OS X (using multiple lines):
sed -ne '/'"$lead"'/ {
p
r realm.xml
:a
n
/'"$tail"'/ {
p
b
}
ba
}
p' server.xml
Both these codes print the output on stdout. Use redirection or, to substitute the file inline, add the option '-i' (on linux) or '-i ""' (on BSD/OS X).