I have two web pages, one page has been created by hand, the other has been published with visual studio 2010 (.aspx). I want to modify the content of these files, replacing a bunch of script tags by a single script tag. To achieve this goal, I simply run some Perl code from a batch file. Here is the Perl code and the HTML before and after substitution :
Perl in a batch :
perl -pi.backup -e "s/<!--\s*<pack>\s*-->.*?<!--\s*<\/pack>\s*-->/<script src=\"pack.js\"><\/script>/s" file.aspx
HTML input :
<!-- <pack> -->
<script src="file1.js" type="text/javascript"></script>
<script src="file2.js" type="text/javascript"></script>
<!-- </pack> -->
HTML output :
<script src="pack.js"></script>
Everything works fine for the hand created file, while the generated file is not updated unless all lines are gathered into one. I guess the issue comes from linebreaks but I can’t figure out why it does work only for the first file since the code is exactly the same.
Your problem is that running Perl with the -p switch causes it to execute the code for each line and print the result. Thus the regex is only seeing one line of the file at a time, and is never able to match the entire pattern.
You could do something like this:
It slurps the whole file into
$_, then performs your substitution and prints the result to the same file.