I needed to put a line of code into a bunch of html pages. I wanted to see exactly what files got changed, so I wrote this script:
#!/usr/bin/perl use warnings; use strict; use File::Find::Rule; #find all html files in specified directory my $dir = "/var/www/site/htdocs/"; my $rule = File::Find::Rule->file->name("*.html")->start( $dir ); #keep track of the changed files in a file open(OUTFILE,">>fixed_files.txt") || die "cant open fixed_files.txt, $ +!\n"; while ( my $html_file = $rule->match ) { rename($html_file, "$html_file.bak") or die; open(my $fh_in, '<', "$html_file.bak") or die; open(my $fh_out, '>', $html_file) or die; while (<$fh_in>) { #add the urchin code if (s|</head>|<script src="http://mysite.org/__utm.js" typ +e="text/javascript"></script>\n</head>|i) { print OUTFILE "$html_file: fixed Urchin code\n"; } print $fh_out $_; } close($fh_in); close($fh_out); } close OUTFILE;

Which worked like a charm. Unfortunately I came to find that I had a version control issue and some files had been updated with the code already and I didn't know it. So I ended up with a lot of pages with this:

<script src="http://mysite.org/__utm.js" type="text/javascript"></scri +pt> <script src="http://mysite.org/__utm.js" type="text/javascript"></scri +pt> </head>

I am trying to figure out how to remove the duplicate line from the files. I've tried many regexes with no success. My last idea was

#fix the urchin code if (s|<script src="http://mysite.org/__utm.js" type="text/ +javascript"></script>\n<script src="http://mysite.org/__utm.js" type= +"text/javascript"></script>|<script src="http://mysite.org/__utm.js" +type="text/javascript"></script>\n|i) { print OUTFILE "$html_file: fixed Urchin code\n"; }

Can someone point me in a more productive direction?
Thanks!


I learn more and more about less and less until eventually I know everything about nothing.

In reply to Regex to undo a regex? by hmbscully

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.