I have to replace a large block of a text (shell script code) in a file with another block of text.
I am impressed with the How can I use sed to replace a multi-line string? answered by antak and Multi-line replace answered by Bruce Ediger
But I have some trouble in using them.
antak already mentioned in his answer that streaming entire file (
1h;2,$H;$!d;g;
) to the buffer is not advisable for large files, because it overload memory.I know
sed
can be used with the block feature to preserve the text outside the block unchanged. I want to use this feature. But if I use,sed -i '/marker1/,/marker2/s/.*/new text (code)/' filename
it will insert new text (code) repeatedly for each stream. Hence I have to make the visual block as one stream, using something similar to what suggested by antak earlier, but for the block (not for entire file).
As mentioned by Bruce Ediger append feature of
ex
which begin witha
end with.
(dot) can be tried, but my new text (code) contain lines begin with dot, which may be considered as the dot of append syntax. How can I use it in this situation?ex
'sdd
'number of lines' may delete multiple lines, but if I have a block between /marker1/ and /marker2/ with the number of lines not fixed (varying) is to be replaced with new text (code), how to do it ?
sed
orex
command works).r
the read file command, to copy the text from another file, if source for the replacement text is in a file.