Compare lines in 2 files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare lines in 2 files
# 1  
Old 11-11-2015
Compare lines in 2 files

I have 2 files with exactly the same information (with header and separated by ";") and what I would like to do is print (for both files!) the columns that are different and also print the "key" column that is equal in the 2 files For example, if

File1:

Code:
key1;aaa;bbb;ccc

key2;ddd;eee;fff

File2:

Code:
key1;aaa;bbb;qqq

key2;ddd;eee;fff

Desired output is: Column 3 is "ccc" in File1 and "qqq" in File2 for key1

I hope it is clear enough. Thanks a lot.

Last edited by vgersh99; 11-11-2015 at 02:01 PM.. Reason: code tags, please!
# 2  
Old 11-11-2015
Try:
Code:
paste -d\; file1 file2 |
awk -F\; '{for(i=2; i<=NF/2; i++) if($i!=$(i+NF/2)) printf "Column %s is \"%s\" in File1 and \"%s\" in File2 for %s\n", i-1, $i, $(i+NF/2), $1}'

Output:
Code:
Column 3 is "ccc" in File1 and "qqq" in File2 for key1

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-11-2015
It worked just fine!!! Thanks a lot!
# 4  
Old 11-11-2015
Slightly different approach:
Code:
awk '{getline X < f; split (X,Y); for (i=2; i<=NF;i++) if ($i != Y[i]) print "Col", i-1, "is \"" $i "\" in", FILENAME, "and \"" Y[i] "\" in", f, "for", $1  }' FS=";" f=file2 file1
Col 3 is "ccc" in file1 and "qqq" in file2 for key1

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 11-11-2015
RudiC's approach is better, because it also reports the file names.. Here is the same approach, the other way around:
Code:
awk -F\; '{split($0,F)} (getline<f)>0{for(i in F) if(F[i]!=$i) printf "Column %s is \"%s\" in File1 and \"%s\" in File2 for %s\n", i-1, F[i], $i, $1}' f=file2 file1

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files and lines which are the same just delete it

I am having a two files and different days, and this is example: file1: 06.09.2017. abcd 123 file2: 07.09.2017. abcd 1234 So what I want is that file2 with today's date contains only 1234, so where is a problem you would ask? Problem is here that I put these commands into routers,. and... (3 Replies)
Discussion started by: tomislav91
3 Replies

2. Shell Programming and Scripting

Compare lines between two files

I have two files I need to compare these two files and take the lines that are common in both the files and consider the line present in second file for my further processing I have used "Awk" along with "FNR and NR" but that is not working gawk -F= ' > FNR==NR {a=$1; next}; > ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

Compare two files with repeated lines

Hi all, I've been trying to write a script to compare two files. This is what I want: file 1: a 1 2 b 5 9 c 4 7 file 2: a a c a b Output: a 1 2 a 1 2 (2 Replies)
Discussion started by: ernesto561
2 Replies

4. Shell Programming and Scripting

Compare files by lines and columns

Inspired by the extremely short awk code from Ygor on this post I wanted to compare two files on only one field. I can't get it to work. Can anybody help on explaining the code and fix the code? My code which does not work: awk 'BEGIN{a=1};a!=1' file1.txt file2.txt >outfile.txt file1.txt... (1 Reply)
Discussion started by: sdf
1 Replies

5. Shell Programming and Scripting

Compare 2 files, 2 lines at a time

Hi Guys, I want to find any differences between packages installed on 2 servers/zones. I have 2 files that contain the output from pkginfo -x . I want to know if any packages exist only in one file and I want to also know about any packages that exist in both but with a different version. ie:... (8 Replies)
Discussion started by: Tornado
8 Replies

6. Shell Programming and Scripting

compare files and then remove some lines

Hi everyone I have a dilemma and I'm hoping someone has an answer for me. I have two files: # cat masterfile line3 line4 line5 line6 line7 # cat tempfile line1 line2 line3 line4 I want to compare tempfile with masterfile. (3 Replies)
Discussion started by: soliberus
3 Replies

7. Shell Programming and Scripting

Compare lines in two files (Memory getting exhausted)

Hi, Could someone please help me with the best approach to compare lines from one file to another? Here is how I have entries - File 1 a1 a2 a3 a4 a9 a10 a15 File2 a5 a6 a15 (5 Replies)
Discussion started by: sncoupons
5 Replies

8. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies

9. Shell Programming and Scripting

compare files by lines and columns

Dear All, Is it possible to compare 2 files line to line using column values? for example I have file1: 1;givi;01012000;wer 2;sss;02012000;rrr 3;ccc;03012000;ttt file 2: 0;uuu;01012000;lll 1;givi;01012000;wer 2;sss;02012000;rrr 3;ccc;03012000;ttt 5;givi;01012000;hhh I want... (4 Replies)
Discussion started by: giviut
4 Replies

10. Shell Programming and Scripting

Trying to compare lines in 2 files

Hello, I am new to scripting and need some help. In looking at other posts on this forum, I came up with the following logic. I cannot figure out why I am getting names of files of the current directory in my echo output. Scenario: message file has a line containing the version. Version.txt... (2 Replies)
Discussion started by: brdholman
2 Replies
Login or Register to Ask a Question