It seems to work for me, unless I misunderstood your specs. Here are the files I am using:
patterns.txt
A[0]
C
D
infile.txt
A[0]
B
C
D[0]
D1
DA
brut.pl
#!/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 @tokens = ();
while (my $line = <$pattern_fh>) {
chomp $line;
push @tokens, quotemeta($line);
}
my $pattern = '^(?:' . join('|', @tokens) . ')[^a-zA-Z]*$';
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_f
+ilename: $!";
while(my $line = <$infile>) {
print "input : $line";
if ($line =~ /$pattern/) {
next;
}
print "output: $line";
print $outfile $line;
}
close($infile);
close($outfile);
perl brut.pl patterns.txt infile.txt outfile.txt
Search pattern: ^(?:A\[0\]|C|D)[^a-zA-Z]*$
input : A[0]
input : B
output: B
input : C
input : D[0]
input : D1
input : DA
output: DA
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.