如何在 Perl 中比较两个文件并显示差异?

发布于 2024-12-16 19:59:42 字数 529 浏览 0 评论 0原文

我正在尝试编写 Perl 脚本来比较 2 个文件的内容,以便它列出所看到的任何差异。尝试以下操作,但我不确定如何继续。请注意,以下只是脚本的一部分,因为我已经预先对这两个文件的内容进行了排序。提前致谢。

open (FILE1, "log") || die ("Can't open file log for reading") ;
open (FILE2, "master") || die ("Can't open file master for reading") ;

@file1 = <FILE1> ;
@file2 = <FILE2> ;

#$perlcompare = (compare('log','master')== 0) ;
#die ("Log and master files are equal and match.\n") ;

if (@file1 eq @file2) {

print "Log and master are equal and match.\n" ;
} else  ????????????

exit 0;

I'm trying to write Perl script to compare the content of 2 files so that it would list out any differences seen. Trying the following but I'm not sure how to continue further. Note that following is only part of the script as I have sorted the content of the 2 files beforehand. Thanks in advance.

open (FILE1, "log") || die ("Can't open file log for reading") ;
open (FILE2, "master") || die ("Can't open file master for reading") ;

@file1 = <FILE1> ;
@file2 = <FILE2> ;

#$perlcompare = (compare('log','master')== 0) ;
#die ("Log and master files are equal and match.\n") ;

if (@file1 eq @file2) {

print "Log and master are equal and match.\n" ;
} else  ????????????

exit 0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

各自安好 2024-12-23 19:59:42

如果你需要留在 Perl 中,有 File::Compare ,它只会比较文件。

为了显示差异,有 Text::Diff

C:\Temp> cat file1
1
2
3
4
5
C:\Temp> cat file2
1
2
3
5
#!/usr/bin/env perl

use strict; use warnings;

use Text::Diff;

my $diffs = diff 'file1' => 'file2';

print $diffs;

输出

C:\Temp> t
--- file1       Fri Nov 18 00:01:40 2011
+++ file2       Fri Nov 18 00:01:49 2011
@@ -1,5 +1,4 @@
 1
 2
 3
-4
+5
-5

If you need to stay within Perl, there is File::Compare which will just compare the files.

For showing differences, there is Text::Diff.

C:\Temp> cat file1
1
2
3
4
5
C:\Temp> cat file2
1
2
3
5
#!/usr/bin/env perl

use strict; use warnings;

use Text::Diff;

my $diffs = diff 'file1' => 'file2';

print $diffs;

Output

C:\Temp> t
--- file1       Fri Nov 18 00:01:40 2011
+++ file2       Fri Nov 18 00:01:49 2011
@@ -1,5 +1,4 @@
 1
 2
 3
-4
+5
-5
喵星人汪星人 2024-12-23 19:59:42

如果您可以使用 perl 之外的其他任何东西,我会推荐 diff(1) 或 comm(1)

comm -3 sorted-file-1 sorted-file-2

If you can use anything else than perl, I would recommend diff(1) or comm(1)

comm -3 sorted-file-1 sorted-file-2
过度放纵 2024-12-23 19:59:42
#!/usr/bin/perl
use strict;
use warnings;
use List::Compare;

open (my $log, "<", "log") or die $!;
open (my $master, "<", "master") or die $!;
my @content_log=<$log>;
my @content_master=<$master>;

my $lc = List::Compare->new(\@content_log, \@content_master);    
my @intersection = $lc->get_intersection;
my @firstonly = $lc->get_unique;
my @secondonly = $lc->get_complement;

print "Common Items:\n"."@intersection"."\n";
print "Items Only in First List:\n"."@firstonly"."\n";
print "Items Only in Second List:\n"."@secondonly"."\n";

print "log\n", $lc->get_unique,"\n"; 
print "master\n", $lc->get_complement,"\n"; 

close $log;
close $master;
#!/usr/bin/perl
use strict;
use warnings;
use List::Compare;

open (my $log, "<", "log") or die $!;
open (my $master, "<", "master") or die $!;
my @content_log=<$log>;
my @content_master=<$master>;

my $lc = List::Compare->new(\@content_log, \@content_master);    
my @intersection = $lc->get_intersection;
my @firstonly = $lc->get_unique;
my @secondonly = $lc->get_complement;

print "Common Items:\n"."@intersection"."\n";
print "Items Only in First List:\n"."@firstonly"."\n";
print "Items Only in Second List:\n"."@secondonly"."\n";

print "log\n", $lc->get_unique,"\n"; 
print "master\n", $lc->get_complement,"\n"; 

close $log;
close $master;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文