总是使用 Perl 的 grep 而不是使用 pipe 更好:
@lines = `zcat $file_list2`; # move output of zcat to array
die('zcat error') if ($?); # will exit script with error if zcat is problem
# chomp(@lines) # this will remove "\n" from each line
foreach $i ( @contact_list ) {
print "$i\n";
@ar = grep (/$i/, @lines);
print @ar;
# print join("\n",@ar)."\n"; # in case of using chomp
}
最好的解决方案不是调用 zcat,而是使用 zlib 库:
http: //perldoc.perl.org/IO/Zlib.html
use IO::Zlib;
# ....
# place your defiiniton of $file_list2 and @contact list here.
# ...
$fh = new IO::Zlib; $fh->open($file_list2, "rb")
or die("Cannot open $file_list2");
@lines = <$fh>;
$fh->close;
#chomp(@lines); #remove "\n" symbols from lines
foreach $i ( @contact_list ) {
print "$i\n";
@ar = grep (/$i/, @lines);
print (@ar);
# print join("\n",@ar)."\n"; #in case of using chomp
}