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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-20-2012
Power Compare two files Field by field and output the result in another file

Hi Friends,

Need Help. I have file1.txt as

File1.txt
Code:
|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
Code:
|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 Smilie I am very new to Shell scripting Smilie .

Last edited by Franklin52; 07-20-2012 at 03:44 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-20-2012
Try...
Code:
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}'

This User Gave Thanks to Ygor For This Post:
i150371485 (07-20-2012)
# 3  
Old 07-20-2012
@Ygor: Did you notice "|" at the beginning of every line?
# 4  
Old 07-20-2012
@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.
# 5  
Old 07-20-2012
Just use i-1 instead of i Smilie
This User Gave Thanks to PikK45 For This Post:
i150371485 (07-20-2012)
# 6  
Old 07-20-2012
Thanks So much Pikk45 Smilie I have just joined the forum to learn things in unix shell scripting and you pepole helping a lot .. Thanks again @ygor and Pikka45 Smilie Smilie
# 7  
Old 07-20-2012
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 Smilie
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Combine Similar Output from the 2nd field w.r.t 1st Field alvinoo UNIX for Dummies Questions & Answers 1 04-28-2016 04:11 AM
Use bash command on awk field and output the result garethsays Shell Programming and Scripting 2 01-07-2016 04:55 PM
Command/script to match a field and print the next field of each line in a file. pocodot Shell Programming and Scripting 10 08-17-2015 08:28 AM
File Compare at field level Saanvi1 Shell Programming and Scripting 5 02-12-2014 01:35 PM
How do I format a Date field of a .CSV file with multiple commas in a string field? dhruuv369 Linux 3 10-16-2013 04:50 PM
Compare two CSV files and put the difference in third file with line no,field no and diff value. karingulanagara Shell Programming and Scripting 12 03-05-2013 04:01 AM
Plz Help. Compare 2 files field by field and get the output in another file. i150371485 Shell Programming and Scripting 5 07-26-2012 12:32 AM
Compare a common field in two files and append a column from File 1 in File2 Santoshbn Shell Programming and Scripting 2 04-26-2012 01:12 AM
Compare Field in Current Line with Field in Previous moutaye Shell Programming and Scripting 9 03-27-2012 05:18 AM
compare two files based on common field in unix PRS UNIX for Dummies Questions & Answers 4 12-07-2011 12:27 PM
Compare two files and output difference, by first field using awk. charles33 Shell Programming and Scripting 2 11-04-2011 12:55 AM
AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2 right_coaster Shell Programming and Scripting 4 10-06-2011 06:07 PM
How to compare 2 field from 2 separated file micxshinoda UNIX and Linux Applications 0 09-19-2011 01:39 AM
AWK to compare two files for each field value Sangtha Shell Programming and Scripting 2 07-17-2009 10:09 AM
Compare 2 files and output result in pop up window hongsh UNIX for Dummies Questions & Answers 2 05-23-2008 09:50 AM