开发者

How to find the difference between 2 files in shell script

There are 100 files in dir and there are 2 pairs of file.

I want to find the difference between 2 files in shell script

File 1: 
Operating S开发者_JAVA百科ystem : Windows XP
Operating System : Windows NT
Operating System : Windows 2008

FILE 2: 
Windows XP
Windows NT
Windows2008

( e.g Windows 2008 ( file 1 ) and Windows2008 ( file2) ) .

But finally both file dont have any difference.

How to achieve this?

These file in linux host and want to do shell script ?


Let's use Perl and diff, shall we? cut doesn't quite do the job. In faithfulness to your original comment, I'm going to look for the word that comes after 'Widnows' in each line of input, and create a new file consisting of only those words. Then I'm going to diff the files.

Every time I've posted Perl, every single time, I've had a swarm of StackOverflowers criticize it. So, get ready for some bad Perl. It will probably work. My reputation can take the downvotes, and I really want to be helpful here.

First, the Perl script (call it 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*/;
}

Now, the command you run:

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

Feel free to make this one big Perl script. Whatever.


The diff utility is on most systems, and the -u unified output is most popular.

$ 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

If you want a word-by-word diff instead, you can use less-common tools such as wdiff:

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

If you want a more visually obvious view of the differences in two files, you can use tools such as xxdiff or kdiff3 or similar. (There are a lot of three-way-merge graphical diff tools.)

If you want something that might be easier to use programmatically, the cmp program can list all the byte dif ferences in files:

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

cmp may be more useful on files that are very nearly identical.


The question is too imprecise, but try this:

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


I would use diff, diff3 if you are comparing 3 files with each other or vimdiff.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜