我是 Perl 的新手。我有八个文本文件,每个文件超过五千行。我想编写一个 perl 脚本来查找在前五个文件中找到但没有找到最后三个文件的条目(记录)。假设文件是 (A, B, C, D, E, F, G, H) 所以我想获取在A
toE
但不是F
to中找到的条目H
。
有人可以就如何为这份工作编写代码提供建议吗?
我是 Perl 的新手。我有八个文本文件,每个文件超过五千行。我想编写一个 perl 脚本来查找在前五个文件中找到但没有找到最后三个文件的条目(记录)。假设文件是 (A, B, C, D, E, F, G, H) 所以我想获取在A
toE
但不是F
to中找到的条目H
。
有人可以就如何为这份工作编写代码提供建议吗?
如果我理解正确,您需要:
您将使用两个散列,而不是使用两个列表。
# Two sets of files to be compared.
my @Set1 = qw(A B C D E);
my @Set2 = qw(F G H);
# Get all the items out of each set into hash references
my $items_in_set1 = get_items(@Set1);
my $items_in_set2 = get_items(@Set2);
my %unique_to_set1;
for my $item (keys %$items_in_set1) {
# If an item in set 1 isn't in set 2, remember it.
$unique_to_set1{$item}++ if !$items_in_set2->{$item};
}
# Print them out
print join "\n", keys %unique_to_set1;
sub get_items {
my @files = @_;
my %items;
for my $file (@files) {
open my $fh, "<", $file or die "Can't open $file: $!";
while( my $item = <$fh>) {
chomp $item;
$items{$item}++;
}
}
return \%items;
}
如果它是一次性的,你可以在 shell 中完成它。
cat A B C D E | sort | uniq > set1
cat F G H | sort | uniq > set2
comm -23 set1 set2
cat A B C D E
将文件一起涂抹成一个流。将其交给sort
,然后uniq
删除重复项(uniq
除非对行进行排序,否则效果不佳)。结果被放入文件set1
中。这对第二组再次进行。 comm
然后在两个集合文件上使用以比较它们,仅显示set1
.