I'm in the process of writing a small Perl script that will take an input file of one line per string value, then open the second file to be updated add entries to it from the input file if entry doesn't exist already, then send output to the third file.

The example use is adding entries to Firefox user.js files. Though I would like to make this generic enough to reuse for other types of files that need updating. We want to define a few entries if they don't already exist. Like: add user_pref("app.update.auto", false); As we have a central software update system for FF.

The input file update.txt would contain.

user_pref("app.update.auto", false); user_pref("app.update.enabled", false); user_pref("autoupdate.enabled", false);
The user.js file, ( file to be updated) looks like
user_pref("browser.bookmarks.file", "h:\\Netscape\\bookmark.htm"); ..
The complex string comparison issue comes into play when trying to compare the input string against that is already there. It Never matches. An exact match that exists in both files never match. So I suspect the special characters found in both the input and file to be updated files. Should I replace the special characters with non special but unique characters then compare? Whats the best way to do the comparison while trying to keep the utility general enough so I can use it against other types of files? Here is the main code:
use warnings; my $fh; my $myfile = 'update.txt'; unless (open($fh,"<",$myfile)) { die "Can't open $myfile: $!\n"; } my %update_words = (); ## Read in update file strings ################################## while (<$fh>) { chomp; $update_words{$_}++; print "$_\n"; } close($fh); ################################# # Find files that need updating ################################# use File::Path; @files = <"c:/documents and settings/*">; foreach $file (@files) { print $file . "\n"; clean_extension($file); } sub clean_extension { # For each of the mozilla profiles in this users profile @exfiles = <"@_/Application Data/Mozilla/Firefox/Profiles/*">; foreach $exfiles (@exfiles) { if ( -e "$exfiles/user.js" ) { print "$exfiles/user.js\n"; open(USERPREF, "$exfiles/user.js") or die "Profile open fa +iled."; # Lines look like: user_pref("app.update.auto", false); while(<USERPREF>) { chomp; if (exists($update_words{$_})) { print "found $_ \n"; # Write } else { print "not found $_ \n"; # write $update_words{$_} to file Outfile } #} } } } }

In reply to complex string matching by freebsdboy

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.