B
Ben Burch
Hi!
I needed to take the email addresses that bounced out of an original
mailing list. grep -v -f was far to slow, and comm produced unexpected
results, and so I just wrote something to do it in perl. Thought this
might be useful to somebody else;
#!/usr/bin/perl
#
# filter $file1 $file2
#
# Filters all lines in file1 against lines in file2, copying only lines
# from file 1 not found in file2 to STDOUT
#
# get arguments
my $file1 = shift;
my $file2 = shift;
if(!defined($file1) || !defined($file2))
{
print "\nError, must have two arguments.\n";
print "filter <masterfile> <excludefile>\n";
exit 1;
}
# Copy all lines from file2 into a hash
open (EXCLUDE, $file2);
my %exclude = ();
while ($line = <EXCLUDE>)
{
chomp($line);
$exclude{$line} = 1;
}
close EXCLUDE;
# Now go through input line-by-line comparing to hash and only
# printing lines that do not match
open (DATA, $file1);
while ($line = <DATA>)
{
chomp($line);
if(!exists($exclude{$line}))
{
print "$line\n";
}
}
close DATA;
exit 0;
I needed to take the email addresses that bounced out of an original
mailing list. grep -v -f was far to slow, and comm produced unexpected
results, and so I just wrote something to do it in perl. Thought this
might be useful to somebody else;
#!/usr/bin/perl
#
# filter $file1 $file2
#
# Filters all lines in file1 against lines in file2, copying only lines
# from file 1 not found in file2 to STDOUT
#
# get arguments
my $file1 = shift;
my $file2 = shift;
if(!defined($file1) || !defined($file2))
{
print "\nError, must have two arguments.\n";
print "filter <masterfile> <excludefile>\n";
exit 1;
}
# Copy all lines from file2 into a hash
open (EXCLUDE, $file2);
my %exclude = ();
while ($line = <EXCLUDE>)
{
chomp($line);
$exclude{$line} = 1;
}
close EXCLUDE;
# Now go through input line-by-line comparing to hash and only
# printing lines that do not match
open (DATA, $file1);
while ($line = <DATA>)
{
chomp($line);
if(!exists($exclude{$line}))
{
print "$line\n";
}
}
close DATA;
exit 0;