簡體   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