I'm a Perl beginner. I usually use "sed" for pattern substitution, but as I need to comment out some multiline sections in a huge file, I thought of using Perl as being more powerful in multiline search.
Pattern Sample:
word1 {
do not care1
do not care2
...
} word2
Intended Output:
#word1 {
# do not care1
# do not care2
# ...
#} word2
I found very useful Q&As on perl multiline matching, but I couldn't find any good/compact/easy solution to add prefix to ALL the matched lines (not just the first line).
For now, this is the code I wrote:
perl -0777 -pe 's/(word1\s*{[^}]*\s*}\s*word2\s.*)/#$1/gm'
But unfortunately it can't add the prefix (#) to all the matched lines as below:
#word1 {
do not care1
do not care2
...
} word2
Can you help please? I hope the code would still be in 1 line :)
Use the range operator:
perl -pe 's/^/#/ if /word1\s*\{/ .. /\}\s*word2/'