繁体   English   中英

Perl:如何比较两个文件?

[英]Perl: how to compare two files?

我试图在比较两个文本文件的perl中制作脚本。 文件之间的差异应与行号一起打印到文件error.txt中。

例:

文件1:

Figure 1.
Somatotropes are organized into.
Figure 2.
Comparing two xml files organized into.
Figure 3.
Somatotropes presentation of GH1,

档案2:

Figure 1.
children with acquired organized into.
Figure 2.
Severe anterior hypoplasia,
Figure 3.
Somatotropes presentation of GH1,

errr.txt中所需的输出:

Error:lineno:2 please check mismatch<br>
Error:lineno:4 please check mismatch<br>

到目前为止,这是我的代码:

use strict;
use warnings;
use Text::Diff;

my $file1 = 'file1.txt';
my $file2 = 'file2.txt';
my $error = 'error.txt';

open(my $in1, '<', $file1) or die "Cannot open file '$file1' for reading: $!";
open(my $in2, '<', $file2) or die "Cannot open file '$file2' for reading: $!";
open(my $out, '>', $error) or die "Cannot open file '$error' for writing: $!";

my $lineno = 1;

while (my $line1 = <$in1>)
{
    my $line2 = <$in2>;

    printf $out "Error:lineno:%d please check mismatch\n", $lineno
        unless $line1 eq $line2;

    ++$lineno;
}

close $out or die "Cannot close file '$error': $!";
close $in2 or die "Cannot close file '$file2': $!";
close $in1 or die "Cannot close file '$file1': $!";
# the logic might be it matches line by line and the whatever mismatch found grab
# the position like line no. and print it in error.txt

my $diff  = diff "file1.txt", "file2.txt";

print $out $diff;
close $out or die "Cannot close file '$error': $!";

这是一个简单的示例:

#!/sur/bin/perl
use strict;
use warnings;

open(FILE,"file1.txt");
my @file1 = <FILE>;
close FILE;
open(FILE,"file2.txt");
my @file2 = <FILE>;
close FILE;

my @errors = ();

for(my $line = 0; $line < scalar(@file1); $line++){
    if($file1[$line] ne $file2[$line]){
        push(@errors, "Error:lineno:".($line+1));
    }
}


open(ERROR,">","error.txt");
foreach(@errors){
    print ERROR $_."\n";
}
close ERROR;

首先,它打开文件并将它们放在数组中,然后在循环中,比较每行,如果它们不同,则在错误数组中推送一条消息。 最后,它将错误放入您的错误文件中。

该代码将在不同大小的文件上失败,我让您实现此功能以及错误声明。

您尝试哪种类型的差异? 您是否假设两个文件的行数相同? 在真正的差异中,您可以假设行不一定总是对齐 让我们看一下这两个文件:

文件1

Line #1
Line #2
FOOBAR!
Line #3
Line #4

文件2

Line #1
FOOBAR!
Line #2
Line #3
Line #4

我们看一下,然后说:“在文件#1中,在行Line #1Line #2之间增加了一条线FOOBAR 。在文件#2中,此行在Line #1Line #2 。在diff程序中,它将说除了FOOBAR行,这些文件几乎相同。

但是,如果我逐行进行比较,我会发现除第一行以外的所有行都是不同的。

在您的程序中,您会逐行进行比较,这非常好。 您使用了许多更现代的语法,并使用strictwarnings 如果我正在编写它,我的循环会有所不同。 我可能会使用一个无限循环,当我用尽任何一个文件中的行时都会中断它:

for (;;) {
    my $line1 = <$in1>;
    my $line2 = <$in2>;
    if    ( not $line1 and $line2 ) {
        say STDERR "ERROR: File #1 is shorter than File #2";
        last;
    }
    elsif ( $line1 and not $line2 ) {
        say STDERR "ERROR: File #2 is shorter than File #1";
        last;
    elsif ( not $line1 and not $line2 ) {
        say "Both files are the same length";
        last
    }
    chomp $line1;
    chomp $line2;
    ...   # Compare the lines, etc.
}

我的理由是,您不知道哪个文件将首先结束,并且一个文件中的每一行的循环都是令人误解的。 您正在读取两个文件,直到其中一个用完为止。 (我还会say我比print和自动autodie更喜欢的autodie因为如果无法打开文件,无论如何您都会死去。

您已经在使用Text::Diff ,它将为您进行文件比较,并且比简单的逐行操作更彻底。 这就是为什么我们使用Perl模块的原因。 好的模块已经在更广阔的领域中进行了测试,并且发现了所有各种例外情况和其他使编程变得如此困难的困难。 预期异常是使编程如此困难的原因。

我将使用Text::Diff并使用它及其配置。 我没用过 但是,可能可以使用其输出(可以捕获),并使用该输出获得所需的输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM