We don't bite newbies here... much | |
PerlMonks |
Re: The best way to replace several fragments of the file starting with the one pattern(marker) and ending with another pattern/marker.by johngg (Canon) |
on Nov 02, 2020 at 13:11 UTC ( [id://11123342]=note: print w/replies, xml ) | Need Help?? |
I slurp the data into a string then use regexen and @- & @+ to find the point just after the start marker and just before the end marker. I then use substr and join to replace the necessary text between the markers, working backwards from the right side of the string by sorting start marker position into descending order, greping away any incomplete marker events. ( Note that I have formed the habit of opening file handles against HEREDOCs rather than using the __DATA__ section as it allows me to use multiple data sources in scripts as necessary. )
The output.
I hope this is of interest. Update: Corrected grep link. Cheers, JohnGG
In Section
Seekers of Perl Wisdom
|
|