This is my file foo.txt:
a
b
This is what I'm doing:
$ perl -pi -e 's/\nb/z/g' foo.txt
Nothing changes in the file, while I'm expecting it to become:
az
Why? It's Perl v5.34.0.
The firs time you evaluate the substitution, you match against a␊. The second time, against b␊. So it doesn't match either times.
You want to match against the entire file. You can tell Perl to consider the entire file one line by using -g aka -0777.
perl -i -gpe's/\nb/z/g' foo.txt # 5.36+
perl -i -0777pe's/\nb/z/g' foo.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With