gpt4 book ai didi

bash - 如何在shell脚本中找到2个文件之间的差异

转载 作者:行者123 更新时间:2023-11-29 09:22:27 29 4
gpt4 key购买 nike

目录下有100个文件,有2对文件。

我想找出 shell 脚本中两个文件之间的区别

File 1: 
Operating System : Windows XP
Operating System : Windows NT
Operating System : Windows 2008

FILE 2:
Windows XP
Windows NT
Windows2008

(例如 Windows 2008(文件 1)和 Windows2008(文件 2))。

但最后两个文件没有任何区别。

如何实现?

这些文件在 linux 主机上,想做 shell 脚本?

最佳答案

让我们使用 Perl 和 diff,好吗? cut 并不能很好地完成这项工作。为了忠实于您的原始评论,我将在每一行输入中查找“Widnows”之后的单词,并创建一个仅包含这些单词的新文件。然后我要比较文件。

每次我发布 Perl,每次,我都会有一大群 StackOverflowers 批评它。所以,准备好迎接一些糟糕的 Perl。它可能会起作用。我的声誉可以承受反对票,我真的很想在这里提供帮助。

首先,Perl 脚本(称之为 preparse.pl):

my $f = shift @ARGV;
open FILE, "<$f" or die("Couldn't open file!");
while (<FILE>) {
print "$1\n" if $_ =~ /Widnows(\s?)*?(\S+)\s*/;
}

现在,您运行的命令:

preparse.pl file1 > file1.tmp
preparse.pl file2 > file2.tmp
diff file1.tmp file2.tmp

请随意制作这个大型 Perl 脚本。随便。

关于bash - 如何在shell脚本中找到2个文件之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3244639/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com