Perl:如何比较两个文件?

时间:2014-08-26 06:35:28

标签: perl

我正在尝试在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': $!";

2 个答案:

答案 0 :(得分:0)

这是一个简单的例子:

#!/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 :(得分:0)

您尝试使用什么类型差异?你假设这两个文件的行数相同吗?在真正的差异中,您可以假设线条可能并不总是排队。我们来看看这两个文件:

文件#1

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

文件#2

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

我们看一下并说“在文件#1中,在FOOBARLine #1之间添加了一行Line #2。在文件#2中,这一行在{{1}之间}和Line #1。在差异程序中,它会说这些文件几乎完全相同,除了Line #2行。

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

在你的程序中,你会逐行区分并且它非常好。你使用FOOBARstrict做了很多更现代的语法。如果我正在写它,我会做一些不同的循环。我可能会使用一个无限循环,当我从任一文件中用完行时,它会突破它:

warnings

我的理由是你不知道哪个文件会先结束,而且一个文件中每行的循环会产生误导。您正在阅读两个文件,直到其中一个文件用完了。 (我也会使用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. } ,我比sayprint要好得多,因为如果无法打开这些文件,你还是会死的。

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

我会使用Text::Diff并使用它及其配置。我从来没用过它。但是,有可能使用它的输出(可以捕获),并使用它来获得你想要的输出。

相关问题