使用Perl搜索两个文件之间的匹配项

时间:2012-04-30 08:25:47

标签: regex string perl file list

我有两个文件:file1.txtfile2.txt。两者都包含以这种格式的行:

文件1

name1:value1

file2的

name2:value2

我想检查value1中是否找到list2name2字符串中)

我有这个功能:

#!/usr/bin/perl
use warnings;
use Parallel::ForkManager;

sub loadf($);

print "Starting main program\n";
my @list1 = loadf("list1.txt");
my @list2 = loadf("list2.txt");

my $workernum = 10; 
open(OK, '>>', 'valid.txt');
open(ER, '>>', 'invalid.txt');
$pm = new Parallel::ForkManager($workernum);

my $cnt = 0;
foreach $line (@list1) {
    $cnt++;
    my $pid = $pm->start and next;
    my @data1 = split(":", $line);
    my $name1 = $data1[0];
    my $value1 = $data1[1];
    my @data2 = split(":", $list2);
    my $name2 = $data2[0];
    my $value2 = $data2[1];

    if (/$value1/i ~~ @list2) 
    {
        print OK $name1 . " - " . $value2 . "\n";
        print " [+] Found: " . $name1 . " - " . $value2 . "\n";
    }
    else
    {
        print ER $name1 . "\n";
        print " [x] Unknown: " . $name1 . " - " . $value1 . "\n";
    }
    $pm->finish;
}

close(OK);
close(ER);
print "\n*** Finished ***\n";

sub loadf($) {
    my @file;
    open(FILE, $_[0] . "\n") or die("[+] Couldn't open " . $_[0] . "\n");
    @file = <FILE>;
    close(FILE);
    return @file;
}

__END__

哪个不行。我做错了什么?

2 个答案:

答案 0 :(得分:1)

嗯...我真的没有得到你的算法,对不起。首先,你将比较文件中的所有行读入两个数组,然后你分支处理器循环,但是,正如我所看到的,不要试图将这些工作者的工作分成块(与它并行)。

我建议尝试一种不同的方法:只啜饮第二个文件,然后逐行处理第一个文件。你没有提到第二个文件的名称和值是否唯一;我认为它们不是,但如果它们可以使程序变得更简单。

open my $caf, '<', 'list2.txt' or die $!, "\n";
my $checkedAgainst = do { local $/;  <$caf>; };

open my $cf, '<', 'list1.txt' or die $!, "\n";

my $workernum = 10; 
$pm = new Parallel::ForkManager($workernum);

while (<$cf>) {
  my $pid = $pm->start and next;
  my ($nameToCheck, $valueToCheck) = split /:/;
  if ($checkedAgainst =~ /^\Q$valueToCheck\E:(.+)$/m) {
    print " [+] Found: $nameToCheck - $1", "\n";
  }
  else {
    print " [x] Unknown: $nameToCheck - $valueToCheck", "\n";
  }
}
$pm->finish;

换句话说,我首先将第二个文件加载到一个大字符串中,然后尝试将其与第一个文件中的行匹配(逐行)。我不知道,你的价值观中会出现什么符号,这就是为什么在那里使用\ Q- \ E(quotemeta运算符)。

UPDATE:试图制作这个代码,无法在我所处的地方进行测试。

答案 1 :(得分:1)

#!/usr/bin/perl

open(F,'list1.txt');
my @list1=<F>;
close(F);
open(F,'list2.txt');
my @list2=<F>;
close(F);
chomp(@list1,@list2);
foreach my $line (@list1)
{
    if ($line=~/.+\:.+/)
    {
        my @data1 = split(":", $line);
        if (my @d2=grep /$data1[1]\:/i,@list2){print " [+] Found: " . $data1[0] . " - " . [split(':',$d2[0])]->[1] . "\n";   }
        else { print " [x] Unknown: " . $data1[0] . " - " . $data1[1] . "\n"; }
    }
}