regex – Change text block with nearest search pattern
Your code is:
perl -0777 -pe 's/(section:[\s\S]*?"MARKER")/\$1 =~ s/"MARKER"/"NEW MARKER"/gr/e' input.txt
This appears to be attempting a nested substitution.
With /e flag, RHS is actual code, so $ should not be escaped:
perl -0777 -pe 's/(section:[\s\S]*?"MARKER")/$1 =~ s/"MARKER"/"NEW MARKER"/gr/e' input.txt
Conversely, delimiters for nested s/// must be escaped (or different):
perl -0777 -pe 's/(section:[\s\S]*?"MARKER")/$1 =~ s\/"MARKER"\/"NEW MARKER"\/gr/e' input.txt
However, this replaces “section…MARKER” by just “NEW MARKER”, which is not what seems to be wanted.
Instead, it is simpler to make use of the capture group directly:
perl -0777 -pe 's/(section:[\s\S]*?)"MARKER"/$1"NEW MARKER"/g' input.txt
or use a lookaround:
perl -0777 -pe 's/section:[\s\S]*?\K"MARKER"/"NEW MARKER"/g' input.txt
Read more here: Source link
