Compare two files Field by field and output the result in another file

Hi Friends,

Need Help. I have file1.txt as

File1.txt

|123|A|7267|Hyder|Cross|Sell|7801
|995|A|7051|2008|Lunar|New|Year|Promotion|7801
|996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801
|997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801

File2.txt

|123|A|7267|Hyder|Cross|Sell|7801
|995|A|7051|2008|Lunar|New|Year|Promotion|7801
|996|A|7021|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801
|997|B|7182|Q1|Feb-Apr|08|Credit|IT|PA|SBA|Campaign|7801
|999|A|6907|MSM|Near|affluent|7801

output:

Difference in Nth line Nth field and dispaly the fields

like in the above example there is a difference in 4th line 2nd field and there is a difference in 4th line 8th field.

Number of Mismatches=2
Differences are
4th line 2nd field
4th line 8th field

Please help :frowning: I am very new to Shell scripting :frowning: .

Try...

paste -d '|' File1.txt File2.txt | awk -F '|' '{c=NF/2;for(i=1;i<=c;i++)if($i!=$(i+c))printf "line %-5s field %s\n",NR,i}'
1 Like

@Ygor: Did you notice "|" at the beginning of every line?

@ygor : Thanks for the quick response. I got the answer but i am getting one count extra than the exact output .

Expected Output :

4th line 2nd field
4th line 8th field

Actual Output :

4th line 3nd field
4th line 9th field

Please help in this .. I tried but i am not getting the exact out.

Thanks in advance.

Note : we have "|" at the beginning of every line.

Just use i-1 instead of i :wink:

1 Like

Thanks So much Pikk45 :slight_smile: I have just joined the forum to learn things in unix shell scripting and you pepole helping a lot .. Thanks again @ygor and Pikka45 :slight_smile: :slight_smile:

What I did is just a solution that anyone can give!! the advisable thing is to learn what that line does exactly!!

Learn bit by bit :slight_smile:

paste -d '|' sort_table.txt tar_table.txt | awk -F '|' '{c=NF/2;for(i=1;i<=c;i++)if($i!=$(i+c))printf "line %-5s field %s orginal %s changed 
%s\n",NR,i-1,$i,$(i+c)}'

I tried the above command i got the output as below.

line 4 field 3 orginal A changed B
line 4 field 9 orginal ITA changed IT

But my Expected output is :

LINE|FIELD|ORIGINAL|CHANGED
4|3|A|B
4|9|ITA|IT

@Pikka45 ,@ygor Please help on this.. I used the command output in a file tried using delimeter '|' . Could you please help me on this ??