3

This is my one-line Perl command in a Bash script. How do I get the s to change across multiple lines?

#!/bin/bash

file=$1
echo "processing $file"
perl -0777pe 's/.*<script[ |>].*<\/script>/<script> \.\.\. <\/script>/g' "$file" >"${file}changed.txt"

I am inputting an XHTML file in this script. The Perl command line works fine when begin script and end script tags are in the same line. Perl does not find the begin script and end script tags when on separate lines.

Is there a problem with <> in a regular expression?

6
  • Is that XML/HTML and regex? Aug 1, 2014 at 20:11
  • Try using the m modifier for multi-line (.../gm)
    – l'L'l
    Aug 1, 2014 at 20:33
  • 1
    Use /s modifier if you want the dot to match \n too. Aug 1, 2014 at 20:34
  • I tried a suggested solution of writing more perl code in the -e. Sure didn't work. stackoverflow.com/questions/16742526/… Aug 1, 2014 at 21:23
  • Be aware that a regex will not handle all cases properly. For example, both of the current answers fail on input like <script>var foo = "</script>";</script>. You really should use a proper [X]HTML parser. Aug 1, 2014 at 21:31

2 Answers 2

5

You're using -0777 which slurps the entire file. Now, all you need to do is add the /s switch to your regular expression so that the any character . will match new lines.

You probably also need to change your regex to be non-greedy .*?, and the regex can be simplified by using assertions and a different delimiter:

#!/bin/bash

file=$1
echo "processing $file"
perl -0777 -pe 's{<script\s*>\K.*?(?=</script>)}{ ... }gs' $file > "${file}changed.txt"

Switches:

  • -0777: Slurp the entire file
  • -p: Creates a while(<>){...; print} loop for each “line” in your input file.
  • -e: Tells perl to execute the code on command line.
3
  • Is there something special about s{. I thought the first s character { was the separator? Aug 1, 2014 at 21:16
  • 2
    Yes, a standard regex using s///. However, it's possible to use both a single alternative delimiter like s!!! or a balanced delimiter like s{}{}.
    – Miller
    Aug 1, 2014 at 21:35
  • I do not follow this example. You do realize the first script tag can be either "<script>" "<script "? I do not know how the insertion of the script tags follows. Aug 1, 2014 at 22:22
3

Try this:

perl -0777pe 's:<script[ |>].*?</script>:<script> ... </script>:gs' "$file"

From the prelre:

s

Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

The non-greed match .*? help with multiple <script> </script> blocks. Without it, (so greed match .*) for the

<script> some </script> <script> some2 </script>

will give only one

<script>...</script>

With the non-greed match (.*?) the for the same input will give

<script>...</script> <script>...</script>
3
  • 1
    You need to explain your changes rather than just give the code change. Aug 1, 2014 at 20:42
  • @JonathanLeffler if you check my other answwers - me usually giving explanation. But in this case, i have a feeling than the OP will understand it... ;) but ok - youre right in general...
    – clt60
    Aug 1, 2014 at 20:44
  • There's no need to escape the dots in the replacement side of the regex. Aug 1, 2014 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.