Using Raku (formerly known as Perl_6)
~$ raku -e 'for lines.join("\n") {.subst(:global, / "<!--SEPARATOR1-->" <(.*?)> "<!--SEPARATOR1-->" /, "\nnew text\nbutterfly\nfoo bar etc.\n" ).put};' file.txt
#OR
~$ raku -e 'for lines.join("\n") {.split(/ <?after "<!--SEPARATOR1-->"> .*? <?before "<!--SEPARATOR1-->"> /).join("\nnew text\nbutterfly\nfoo bar etc.\n" ).put};' file.txt
Sample Input:
sometext
sometext
<!--SEPARATOR1-->
text to be deleted
foobar to be deleted
foooooooooooobar
foobaaaaaaaaaaar
<!--SEPARATOR1-->
sometext
sometext
Sample Output:
sometext
sometext
<!--SEPARATOR1-->
new text
butterfly
foo bar etc.
<!--SEPARATOR1-->
sometext
sometext
The code above is a fairly-conventional approach using Raku, a member of the Perl-family of programming languages. Briefly (first example), lines
are read-in, join
ed with \n
newlines, and the SEPARATOR
pattern is searched for (paired), with a .*?
non-greedy match to zero-or-more characters in between. If found the <(
…)>
capture markers drop the match outside of <(.*?)>
, which gets substituted with the desired lines.
The second example uses a split
/join
approach, but is otherwise very similar. Each approach gives the same output.
https://raku.org
sed "/pattern/,/pattern/{$!{N; s/pattern/asd/g}}"
but not work – Muhammed Taha Oct 16 '22 at 08:59