regex - Replacing only first block of text between dilimiters with content of file using sed -
i have been tasked replacing license boiler plate text in large number of files. because there many i'd script it, , ideally in one-liner using sed
.
i know this similar question can use like:
find . -type f -exec \ sed -i -ne '/^\/\/ dom-ignore-begin/ {p; r /path/to/new/license.txt' \ -e ':a; n; /^\/\/ dom-ignore-end/ {p; b}; ba}; p' '{}' \;
which find files , replace between ^// dom-ignore-begin
, ^// dom-ignore-end
content of replacement license file. , that's fine , dandy, works charm.
the problem is, of files contain multiple dom-ignore-* blocks, new license replaces whatever in blocks - far ideal.
so i'd know how can limit replacement on first block found , skip rest. regex-fu lacking in respect.
sample input:
blah blah blah blah blah // dom-ignore-begin foo foo foo foo // dom-ignore-end blah blah blah blah // dom-ignore-begin foo foo foo foo foo foo foo foo foo // dom-ignore-end blah blah
expected output:
blah blah blah blah blah // dom-ignore-begin bar bar <- bar bar <- changed // dom-ignore-end blah blah blah blah // dom-ignore-begin foo foo <- foo foo foo foo <- not changed foo foo foo <- // dom-ignore-end blah blah
this might work (gnu sed):
sed -i -e 'x;/./{x;b};x;/dom-ignore-begin/,/dom-ignore-end/{/dom-ignore-end/!d;h;rnewlicensefile' -e 'd}' file
this uses hold space flag prevent further processing of file.
to keep dom-ignore-begin/dom-ignore-end
tags use:
sed -i -e 'x;/./{x;b};x;/dom-ignore-begin/,/dom-ignore-end/{/dom-ignore-begin/{p;rnewlicensefile' -e '};/dom-ignore-end/!d;h}' file
Comments
Post a Comment