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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



mauricev
New User

May 27, 2014, 3:31 PM

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

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


Code
<script type="text/javascript"> 
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<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:

Code
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.


BillKSmith
Veteran

May 28, 2014, 5:27 AM

Post #2 of 2 (23521 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,
Bill

 
 


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

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