2010-07-14 63 views
3

dir中有100个文件,并且有2对文件。如何在shell脚本中找到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))2个文件之间的差异。

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

如何实现这一目标?

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

+0

这个问题很不清楚。它是100个文件还是两个文件,你想比较? “shell脚本”是什么意思?这是一个Unix术语;除非使用像Cygwin这样的Unix模拟环境,否则Windows只有批处理,命令和PowerShell脚本。 – 2010-07-14 08:56:50

+0

in dir there will File1.host.pg1.txt and file1.host.pg2.txt and file1.host2.pg1.txt .... like this – Tree 2010-07-14 08:58:25

+0

我想在linx做 – Tree 2010-07-14 09:00:52

回答

2

我们用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脚本。随你。

1

的问题是太不精确,但试试这个:

diff <(sed 's/Operating System : //' file1.txt) file2.txt 
2

diff工具在大多数系统上,而-u统一的输出是最流行的。

$ diff -u file1 file2 
--- file1 2010-07-14 02:08:20.000000000 -0700 
+++ file2 2010-07-14 02:08:29.000000000 -0700 
@@ -1,3 +1,3 @@ 
-Operating System : Windows XP 
-Operating System : Windows NT 
-Operating System : Windows 2008 
+Windows XP 
+Windows NT 
+Windows2008 

如果你想有一个字的字DIFF相反,你可以使用不常见的工具,如wdiff

$ wdiff file1 file2 
[-Operating System :-]Windows XP 
[-Operating System :-] 
Windows NT 
[-Operating System : Windows 2008-] 
{+Windows2008+} 

如果你想有一个更直观明显鉴于两个差异文件,您可以使用工具,如xxdiffkdiff3或类似的。 (有一个的很多三路合并图形比较工具。)

如果你想要的东西,可能会更容易编程方式使用,该cmp程序可以在文件中列出的所有字节DIF ferences:

$ cmp -l file1 file2 
1 117 127 
2 160 151 
3 145 156 
... 

cmp对于非常接近相同的文件可能更有用。

1

如果您正在对比3个文件或vimdiff,我会使用diff,diff3。