note
imp
In addition to [davorg]'s advice above you should also always use both strict and warnings, as they can help you identify many common problems.
<p>
If you are searching for the words from file A in file B then you will need a different regex. The code you provided is using the entire file A as the regex.
<p>
Here's an example that uses one pattern file, one input file, one output file:
<code>
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 $pattern_filename: $!";
my @tokens = ();
while (my $line = <$pattern_fh>) {
push @tokens, split /\s/, $line;
}
# Create a pattern with alternation of tokens, wrapped in a non-capturing group,
# and a requires word break before and after the word to prevent matching pieces
# of other words
my $pattern = '\b(?:' . join('|', @tokens) . ')\b';
print "Search pattern: $pattern\n";
open my $infile, "<", $source_filename or die "Failed to open $source_filename: $!";
open my $outfile,">>", $dest_filename or die "Failed to open $dest_filename: $!";
while(my $line = <$infile>) {
if ($line !~/$pattern/) {
print "adding: $line";
print $outfile $line;
}
}
close($infile);
close($outfile);
</code>
604526
604526