Here's an approach that initializes a hash of output tokens, prepopulated with marker text for new additions. The output data is overridden for existing entries, and a sorted list is appended to the specified output file (open with '>' instead of '>>' if you don't want this). Note that it isn't fully compatible with the delete code I posted earlier, since that code didn't take comments into account.
#!/usr/local/bin/perl
use strict;
use warnings;
if (@ARGV != 3) {
print "Usage: $0 <pattern file> <input file> <output file>\n";
exit;
}
my ($pattern_filename, $source_filename, $dest_filename) = @ARGV;
open my $pattern_fh, '<', $pattern_filename or die "Failed to open $pa
+ttern_filename: $!";
my %output_tokens = ();
while (my $line = <$pattern_fh>) {
chomp $line;
$output_tokens{$line} = "$line # Added by script";
}
print "Expected tokens: ", join(', ', keys %output_tokens), "\n";
open my $infile, "<", $source_filename or die "Failed to open $source
+_filename: $!";
open my $outfile,">>", $dest_filename or die "Failed to open $dest_f
+ilename: $!";
while(my $line = <$infile>) {
chomp $line;
$output_tokens{$line} = $line;
}
for my $token (sort keys %output_tokens) {
print $output_tokens{$token}, "\n";
}
close($infile);
close($outfile);
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.