Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the difference between 2 files in shell script

Tags:

bash

shell

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 System : 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 ?

like image 601
Tree Avatar asked Dec 06 '25 07:12

Tree


1 Answers

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.

like image 124
Borealid Avatar answered Dec 07 '25 20:12

Borealid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!