CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Regular Expressions:
Yet another multiple newline problem


New User

May 27, 2014, 3:31 PM

Post #1 of 2 (37691 views)
Yet another multiple newline problem Can't Post

I want to find and replace the following section in a file

<script type="text/javascript"> 
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-13163007-1"); pageTracker._trackPageview();
} catch(err) {}</script>

I got perl to do it for the first line separately and for the second line separately, but both lines together it doesn't match.

The original file was DOS and I converted it to UNIX, so the linebreaks should be just newlines, though I previously tried to \r\n to no avail.

Here is the code intended to replace both the first and second lines:

perl -pi -e 's|<script type=\"text/javascript\">\nvar gaJsHost = \(\(\"https:\" == document\.location\.protocol\) \? \"https://ssl\.\" : \"http://www\.\"\);|it_changed|' thefile

Somehow, the linebreak is tripping it up.


May 28, 2014, 5:27 AM

Post #2 of 2 (37409 views)
Re: [mauricev] Yet another multiple newline problem [In reply to] Can't Post

There is nothing wrong with your regular expression. Your problem is with the perl runtime flag -p. It reads the file one line of text (not line of html) at a time. You can fix this (for this sample) by setting $/="</script>\n"; in a BEGIN block.

It seldom is a good idea to edit html with a regular expression. Use a module to parse it first.
Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives