Many time we come across situation that we want exact diff between two files ..
There are various Linux command which we can use like
# diff
# cmp
EG:
# cat 1
1
2
3
4
5
6
# cat 2
2
3
6
7
8
### Using diff
# diff -iw 1 2
1d0
< 1
4,5d2
< 4
< 5
6a4,5
> 7
> 8
Expln:
1d0< 1
- Add "1" at line 1 of file 2
4,5d2
< 4
< 5
- Add "4" at line 4 of file 2
- Add "5" at line 5 of file 2
6a4,5
> 7
> 8
- Add "7" at line 7 of file 2
- Add "8" at line 8 of file 2
-- Actual count as line 0 - N
Here it's tough to found whats mean of above if you are using it first time, What we expect is I want only those line which are missing in file 2 .
So have written a small PerlScript to achieve so, it's possible with BashScript too, but to parse a large file BashScript will take more time .
## How to use
There are various Linux command which we can use like
# diff
# cmp
EG:
# cat 1
1
2
3
4
5
6
# cat 2
2
3
6
7
8
### Using diff
# diff -iw 1 2
1d0
< 1
4,5d2
< 4
< 5
6a4,5
> 7
> 8
Expln:
1d0< 1
- Add "1" at line 1 of file 2
4,5d2
< 4
< 5
- Add "4" at line 4 of file 2
- Add "5" at line 5 of file 2
6a4,5
> 7
> 8
- Add "7" at line 7 of file 2
- Add "8" at line 8 of file 2
-- Actual count as line 0 - N
Here it's tough to found whats mean of above if you are using it first time, What we expect is I want only those line which are missing in file 2 .
So have written a small PerlScript to achieve so, it's possible with BashScript too, but to parse a large file BashScript will take more time .
Script : diff.pl
#!/usr/bin/perl
# Written By - Shirish
#### perl diff.pl file1 file2 OUTPUT
#### Will give you exact diff between file1 and file2 in OUTPUT file
##### Only lines that are in file1 but missing in file2
#################
#open a, "<filea";
#open b, "<fileb";
$x = $ARGV[0];
$y = $ARGV[1];
$z = $ARGV[2];
open a, "< $x";
open b, "< $y";
open (OP, ">$z") || die ("Unable to create Report");
local $/;
my @a = split /\n/, <a>;
my @b = split /\n/, <b>;
my %b = map { $_ => 1 } @b; # Make hash of B
my @res = grep { !defined $b{$_} } @a; # Everything in A not in B
print OP join "\n", @res;
#print join "\n", @res;
print "\n";
# Written By - Shirish
#### perl diff.pl file1 file2 OUTPUT
#### Will give you exact diff between file1 and file2 in OUTPUT file
##### Only lines that are in file1 but missing in file2
#################
#open a, "<filea";
#open b, "<fileb";
$x = $ARGV[0];
$y = $ARGV[1];
$z = $ARGV[2];
open a, "< $x";
open b, "< $y";
open (OP, ">$z") || die ("Unable to create Report");
local $/;
my @a = split /\n/, <a>;
my @b = split /\n/, <b>;
my %b = map { $_ => 1 } @b; # Make hash of B
my @res = grep { !defined $b{$_} } @a; # Everything in A not in B
print OP join "\n", @res;
#print join "\n", @res;
print "\n";
## How to use
## What's in file 1 which are absent in file 2
# perl diff.pl 1 2 1-2
# cat 1-2
1
4
5
# perl diff.pl 1 2 1-2
# cat 1-2
1
4
5
## What's in file 2 which are absent in file 1
# perl diff.pl 2 1 2-1
# cat 2-1
7
8
# perl diff.pl 2 1 2-1
# cat 2-1
7
8
Great Sir ji !
ReplyDelete