Difference in two files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Difference in two files
# 1  
Old 05-17-2013
Difference in two files

Hi all,
I have problem in finding the difference. suppose i have two file
Code:
file1                file2
1xyz               1xyz
1asd               1xyz 
1asd               1abc
2adf                1asd
3asd               2abd
 4abc              2adf 
                      3asf

I need to find the 1xyz etc from file 2 which are not present in file1
Code:
                          1abc

1asd etc from file1 which is not present in file 2.
Code:
                          4abc

I would like to save both in same file3 with 2 coulmn in with the name file1 and file2
like
Code:
file1          file2
1xyz         1asd
1abc         4abc

Thanking you,
XXLMMN

Last edited by Scott; 05-20-2013 at 04:28 AM.. Reason: Code tags
# 2  
Old 05-17-2013
Please use code tags for posting code fragments or data samples.

An awk approach:
Code:
awk '
        NR == FNR {
                A[$1]
                next
        }
        {
                B[$1]
                next
        }
        END {
                for ( k in A )
                {
                        if (!( k in B ))
                                F1[++i] = k
                }
                for ( k in B )
                {
                        if (!( k in A ))
                                F2[++j] = k
                }
                n = ( i > j ) ? i : j
                print "file1", "file2"
                for ( k = 1; k <=n; k++ )
                        print F2[k],F1[k]

        }
' OFS='\t' file1 file2

Output:
Code:
file1   file2
1abc    3asf
2abd    4abc
        3asd

Redirect the output to file3 if required.
# 3  
Old 05-20-2013
Thank you Yoda for the script but using this i am not able to get the name which are in both file but less in one. suppose:
Code:
file1 	file2
1abc	1abc
1abc	1abc
1abc	2abc

Then i would like to get
Code:
file1    file2
1abc   2abc

My file are sorted and i need final file to be sorted.
thanking you ,
regards,
XXLLMN

Last edited by Scott; 05-20-2013 at 04:29 AM.. Reason: Please use code tags
# 4  
Old 06-07-2013
sdiff file1 file2
# 5  
Old 06-07-2013
Quote:
Originally Posted by Yoda
Please use code tags for posting code fragments or data samples.

An awk approach:
Code:
awk '
        NR == FNR {
                A[$1]
                next
        }
        {
                B[$1]
                next
        }
        END {
                for ( k in A )
                {
                        if (!( k in B ))
                                F1[++i] = k
                }
                for ( k in B )
                {
                        if (!( k in A ))
                                F2[++j] = k
                }
                n = ( i > j ) ? i : j
                print "file1", "file2"
                for ( k = 1; k <=n; k++ )
                        print F2[k],F1[k]

        }
' OFS='\t' file1 file2

Output:
Code:
file1   file2
1abc    3asf
2abd    4abc
        3asd

Redirect the output to file3 if required.
Awesome... as usual !

Btw, if I create a file having above awk script in it (need .awk extension??), how to execute the file?
./awk_script <input_file> ??
# 6  
Old 06-07-2013
With sorted files:
Code:
comm -3 file1 file2

Regards,
Alister

Last edited by alister; 06-07-2013 at 03:54 PM..
# 7  
Old 06-07-2013
Quote:
Originally Posted by juzz4fun
Awesome... as usual !

Btw, if I create a file having above awk script in it (need .awk extension??), how to execute the file?
./awk_script <input_file> ??
Code:
awk -f awk_script

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. Shell Programming and Scripting

How to get the difference with 4 files?

HI Team, Could you please help me here. I need to compare 4 files a) 1 2 3 4 b) 1 5 3 4 c) 1 2 5 (1 Reply)
Discussion started by: madhuchilipi
1 Replies

3. UNIX for Advanced & Expert Users

Difference between two files?

I need the get the difference between 2 files and the result, I need to output it to an other file file1 1 1 2 file2 1 1 2 3 Result....File3=3 used diff file1 file2 > file3 ( not working as expected)..Exact requirement..I have 2 flatfiles to compare and load only the difference in a... (0 Replies)
Discussion started by: win4luv
0 Replies

4. Shell Programming and Scripting

difference between two files

i have two files A and B and i want the difference to be printed in file C as in example below File A 10E32E7BCC5881DDB6||002878|1|1| 10E32E7BCC5881DDB7||002878|1|1| 10E32E7BCC5881DDB8||002878|1|1| 10E32E7BCC5881DDB9||002878|1|1| File B 10E32E7BCC5881DDB1||002878|1|1|... (4 Replies)
Discussion started by: tomjones
4 Replies

5. Shell Programming and Scripting

difference in files.

Hi All, **************************************************************** File1: CREATE TABLE "TESTIHUB"."CINEMA" ( "NAME" VARCHAR2(20), "ACTOR" VARCHAR2(20), "YEAR" NUMBER ); / CREATE TABLE "TESTIHUB"."PANT" ( "CNAME" VARCHAR2(20), "AMOUNT" NUMBER, ... (1 Reply)
Discussion started by: naree
1 Replies

6. Shell Programming and Scripting

Difference between two files

HI Folks, sorry if it is a naive question. i have two files . "file1.txt" & "file2.txt" contents in files: file1.txt file2.txt -------- --------- 1 ... (2 Replies)
Discussion started by: babusek
2 Replies

7. Shell Programming and Scripting

Difference between files

I have two files like follows: - File 1: - A 20 B 15 D 10 C 6 File 2: - B 21 A 20 C 11 D 10 Now I want file like this: - It should pick up 'A' from File 1 and pick up 'A' from File 2 and should get difference of the numbers in 2nd column and display result as follows ... (2 Replies)
Discussion started by: balu_puttaganti
2 Replies

8. Shell Programming and Scripting

Difference of more than 2 files....

Hi All, I need your help to find out the difference between 3 files. How can i get the output which says that FILE2 contains additional (YYY,900,100) compare to FILE1 and FILE3 contains addition record (ZZZ,904,908) compare to FILE1? I have tried using diff3 command, but was not able to get... (2 Replies)
Discussion started by: Amit.Sagpariya
2 Replies

9. Shell Programming and Scripting

Difference between .pm and .pl files

I know the basic difference between .pm and .pl files. But wat puzzles me is when to go for creating a module and when to create .pl files.Based on wat criteria we sholud decide to make a function whcih implements a common functionality as module or a .pl file ?? (3 Replies)
Discussion started by: Anuj8584
3 Replies

10. Shell Programming and Scripting

difference b/w files

i have 2 files file 1---------> hello file 2---------> hello world is there any command that we find the diff b/w two files and gives the ans as what is not matched with file 2. diff file 1 file 2 here the desired o/p should be: world as "world" is not found in file 1. (2 Replies)
Discussion started by: ali560045
2 Replies
Login or Register to Ask a Question