Skip to content

Instantly share code, notes, and snippets.

@ponsfrilus
Last active January 20, 2024 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponsfrilus/01f134ee6e22c52066b9089094ec31ef to your computer and use it in GitHub Desktop.
Save ponsfrilus/01f134ee6e22c52066b9089094ec31ef to your computer and use it in GitHub Desktop.
Replacement between markers

Replacement between markers

I'm looking for a sed command that replace a part of the target.md file identified by the <!-- start:block --> and <!-- end:block --> markers.

This part of the file has to be replaced either with replaceA.md or replaceB.md, but the marker have to be there at the end, so we can start the process again.

You can clone this gist to get the files:

git clone https://gist.github.com/01f134ee6e22c52066b9089094ec31ef.git replace-between-markers

Files

Some text before

<!-- start:block -->
These lines should
be replaced from the
content of another file
(replace.md).
<!-- end:block -->

Some text after
Yes! Replacement text
without the 2 markers
in it.
<!-- start:block -->
Yes! Replacement text
with the 2 markers
in it.
<!-- end:block -->

Yes! Replacement text without the 2 markers in it.

Yes! Replacement text with the 2 markers in it.

Some text before

These lines should be replaced from the content of another file (replace.md).

Some text after

@ponsfrilus
Copy link
Author

awk '/<!-- start:block -->/{p=1;print;system("cat replaceA.md");next} /<!-- end:block -->/{p=0} !p' target.md > temp && mv temp target.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment