This is a simplified description of MyTextString.txt:
Note: BlaBla stands for any character including new line character.
START BlaBla-In END BlaBla-Out-Between START BlaBla-In END BlaBla-Out-Between START BlaBla-In END BlaBla-Out-Between START BlaBla-In END ...
I'm looking for removing text between END and START (BlaBla-Out-Between) to result like this:
START BlaBla-In END newline START BlaBla-In END newline START BlaBla-In END newline START BlaBla-In END ...
I've a perl file changes.pl:
BEGIN {
@ARGV = map glob("\"$_\""), @ARGV;
}
s/(END).*?(START)/$1\n$2/sg; #TEST
I should execute my replaces using this CMD line:
perl -i.bak -p changes.pl My/File/Directory/MyTextString.txt
Note: the changes.pl and CMD line are working well as described in this question with other RegEx find and replace strings.
But with this RegEx string no modifications happen to MyTextString.txt:
s/(END).*?(START)/$1\n$2/sg;
I think every thing regarding my regular expression syntax is OK as it's working well on regex 101 tester.
I'm looking for matching and replacing any character (including newline) using mentioned changes.pl and CMD line. Simply, I'm looking for replacing BlaBla-Out-Between with newline.
You need to slurp the whole file into a string before doing the substitution. The
-p
command line switch only reads a line at a time.It means that the substitution
s/(END).*?(START)/$1\n$2/sg
will only delete anything in those cases where there is anEND
pattern followed by aSTART
pattern on the same single line.To slurp the file you can specify an input record separator of octal
0777
:From perlrun: