It never crossed my mind that ed could be used in such way, this is beautiful.
For people unfamiliar with ed, I highly recommend this little gold nugget
Expanding further on this, I'd figure one could easily replace a block of text with another file using this technique.
For example, you have some inline css in multiple html files and would like to replace the following block of code:
<style>
body {
color: blue
}
</style>
with another file named red.css containing:
<style>
body {
color: red
}
</style>
For all html files in the current directory:
$ for file in *.html
$ do
$ printf '/style\n.,/style/d\n-r red.css\nw\n' | ed -s $file
$ done
Note:
I'd recommend to first test this on one file and write the changes to a new file in /tmp. Then run a diff to see what changes will be made before actually editing the files:
$ printf '/style\n.,/style/d\n-r red.css\nw /tmp/edit\n' | ed -s index.html && diff -bu index.html /tmp/edit