0

I am writing a script myself. But I need your help

I have a file as below (test.md):

sometext
sometext

<!--SEPERATOR1--> text to be deleted foobar to be deleted foooooooooooobar foobaaaaaaaaaaar <!--SEPERATOR1-->

sometext sometext

I want to clean the texts in the sections separated by 'SEPERATOR1' in this text file (test.md) and add new texts. How can I do this, thanks.

Expected output:

sometext
sometext

<!--SEPERATOR1--> new text butterfly foo bar etc. <!--SEPERATOR1-->

sometext sometext

2 Answers2

3

You can use sed specifying a range with /startpattern/,/endpattern/ with the c command to change the text:

sed '/<!--SEPERATOR1-->/,/<!--SEPERATOR1-->/c<!--SEPERATOR1-->\nnew text\nbutterfly\nfoo bar etc.\n<!--SEPERATOR1-->' filename.txt

Use \n for newlines in the result.

You can add > newfilename.txt at the end to redirect the output to a file, or add the -i flag to change the file in place (though the exact syntax for this varies between versions of sed).

But may I also recommend changing SEPERATOR to SEPARATOR — if someone else with more a "conventional" approach to spelling has to maintain your code later on, which could even be an older version of you, something like that could be very annoying.

frabjous
  • 8,691
0

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, joined 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

jubilatious1
  • 3,195
  • 8
  • 17