如何在 Perl 中比较两个文件并显示差异?
我正在尝试编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你需要留在 Perl 中,有 File::Compare ,它只会比较文件。
为了显示差异,有 Text::Diff。
输出
If you need to stay within Perl, there is File::Compare which will just compare the files.
For showing differences, there is Text::Diff.
Output
如果您可以使用 perl 之外的其他任何东西,我会推荐 diff(1) 或 comm(1)
If you can use anything else than perl, I would recommend diff(1) or comm(1)