如何比较两个文本文件并删除匹配的内容并传递到perl的输出? [英] How to compare two text files and removing the matching contents and pass to output in perl?
问题描述
我有两个文本文件text1.txt和text2.txt,如下所示
I have two text files text1.txt and text2.txt like below
text1
ac
abc
abcd
abcde
text2
ab
abc
acd
abcd
输出
$ b
output
ac
abcde
我需要比较这两个文件,并在第二个文件中匹配时从 text1
中删除内容。
I need to compare the two files and remove the content from text1
when there is a match in the second file.
我想要Perl中的代码。目前我正在尝试下面的代码。
I want the code in Perl. Currently I am trying the below code.
#!usr/bin/perl
use strict;
use warnings;
open (GEN, "text1.txt") || die ("cannot open general.txt");
open (SEA, "text2.txt") || die ("cannot open search.txt");
open (OUT,">> output.txt") || die ("cannot open intflist.txt");
open (LOG, ">> logfile.txt");
undef $/;
foreach (<GEN>) {
my $gen = $_;
chomp ($gen);
print LOG $gen;
foreach (<SEA>) {
my $sea = $_;
chomp($sea);
print LOG $sea;
if($gen ne $sea) {
print OUT $gen;
}
}
}
text1
的内容,而不是不匹配的内容。请帮助我。
In this I am getting all content from text1
, not the unmatched content. Please help me out.
推荐答案
您的主要问题是您未定义输入记录分隔符 $ /
。这意味着整个文件将被读取为一个单一的字符串,你可以做的是说两个文件是不同的。
Your main problem is that you have undefined the input record separator $/
. That means the whole file will be read as a single string, and all you can do is say that the two files are different.
undef $ /
和事情会工作得更好。然而, for 循环的内部将读取和打印
file2
中的所有行不匹配的第一行 file1
。第二次遇到此循环时,所有数据都已从文件中读取,因此循环体将不会被执行。您必须在外层循环中打开 file2
或将文件读入数组,然后循环。
Remove undef $/
and things will work a whole lot better. However the inner for
loop will read and print all the lines in file2
that don't match the first line of file1
. The second time this loop is encountered all the data has been read from the file so the body of the loop won't be executed at all. You must either open file2
inside the outer loop or read the file into an array and loop over that instead.
然后,你真的想打印 file2
中不等于 file1
中每一行的所有行吗?
Then again, do you really want to print all lines from file2
that aren't equal to each line in file1
?
更新
正如我在评论中写道,以输出 text2
中不出现的任何位置的 text1
中的行。这很容易实现使用散列:
As I wrote in my comment, it sounds like you want to output the lines in text1
that don't appear anywhere in text2
. That is easily achieved using a hash:
use strict;
use warnings;
my %exclude;
open my $fh, '<', 'text2.txt' or die $!;
while (<$fh>) {
chomp;
$exclude{$_}++;
}
open $fh, '<', 'text1.txt' or die $!;
while (<$fh>) {
chomp;
print "$_\n" unless $exclude{$_};
}
使用您在问题中显示的数据产生此输出
With the data you show in your question, that produces this output
ac
abcde
这篇关于如何比较两个文本文件并删除匹配的内容并传递到perl的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!