Question

I'm trying to make an automated build and use Perl in it to update some paths in a file.

Specifically, in an html file, I want to take the block shown below

<!-- BEGIN: -->
<script src="js/a.js"></script>
<script src="js/b.js"></script>
<script src="js/c.js"></script>
<!-- END: -->

and replace it with

<script src="js/all.js"></script>


I have tried a few regexes like:

perl -i -pe  's/<--BEGIN:(.|\n|\r)*:END-->/stuff/g' file.html

or just starting with:

perl -i -pe  's/BEGIN:(.|\n|\r)*/stuff/g' file.html

But I can't seem to get past the first line. Any ideas?

Was it helpful?

Solution

perl -i -pe 's/<--BEGIN:(.|\n|\r)*:END-->/stuff/g' file.html

This is so close.

  • Now just match with the /s modifier, this allows . to match any char, including newlines.
  • Most importantly, you want to start the match with <!--, note the !.
  • Also, you want a non-greedy match like .*?, in case you have multiple END markers.
  • Your example input shows that there may be extra spaces.

This would lead to the following substitution:

s/<!--\s*BEGIN:.*?END:\s*-->/stuff/sg

As @plusplus pointed out, the -p iterates over each line. Let's change Perl's concept of a “line” to “the whole file at once”:

BEGIN { $/ = undef }

or use the -0 command line switch, without a numeric argument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top