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.
The user.js file, ( file to be updated) looks likeuser_pref("app.update.auto", false); user_pref("app.update.enabled", false); user_pref("autoupdate.enabled", false);
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:user_pref("browser.bookmarks.file", "h:\\Netscape\\bookmark.htm"); ..
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |