Not having your input files, you are going to have to check to see if this works for the types of patterns you have in your files.
See if this helps
#!/usr/bin/perl
use strict;
use warnings;
use IO::Compress::Gzip qw(gzip $GzipError);
if ( !defined($ARGV[0]) ||
!defined($ARGV[1]) ||
!defined($ARGV[2])){
print "Usage: $0 <patterns.gz> <search.gz> <output.gz>\n";
exit;
}
my $href = {};
my $outfile = $ARGV[2];
loadHash($href,$ARGV[0]);
$outfile .= ".gz" unless ( $ARGV[2] =~ /\.gz$/);
checkFile($href,$ARGV[1],$outfile);
##### Subs
sub loadHash {
my ($ref,$file) = @_;
my $fh;
if ( $file =~ /\.gz/ ) {
open($fh, "gunzip -c $file |") or
die "Unable to open pipe to $file\n";
}
else {
open($fh, $file) or die "Unable to open pipe to $file\n";
}
while(<$fh>) {
$ref->{$1} = 1 if ( /^(\S+)/);
}
close($fh);
}
sub checkFile {
my ($ref,$file,$output) = @_;
my $fh;
if ( $file =~ /\.gz/ ) {
open($fh, "gunzip -c $file |") or
die "Unable to open pipe to $file\n";
}
else {
open($fh, $file) or die "Unable to open pipe to $file\n";
}
my $z = new IO::Compress::Gzip $output
or die "gzip failed: $GzipError\n";
while(<$fh>){
if ( /^(\S+),/) {
$z->print($_) if (defined($ref->{$1}));
}
}
$z->close();
close($fh);
}
Pattern file:
$ gunzip -c pat.gz
abc
123
bbb
333
890
ddd
Search file:
$ gunzip -c search.gz
This,
is,
a,
test,
abc,Got the right line
to,
see,
if,
I,
bbb, Got another line
can,
make,
this,
work,
Output:
$ gunzip -c output.gz
abc,Got the right line
bbb, Got another line