我有两个文件:file1.txt
和file2.txt
。两者都包含以这种格式的行:
文件1
name1:value1
file2的
name2:value2
我想检查value1
中是否找到list2
(name2
字符串中)
我有这个功能:
#!/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__
哪个不行。我做错了什么?
答案 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"; }
}
}