Operating System - Linux
1753665 Members
5381 Online
108798 Solutions
New Discussion юеВ

Re: Need to find the difference of two files and redirects to a third file

 
Kumar_143
New Member

Need to find the difference of two files and redirects to a third file

File is having million of records. I need to find the differnce of the two files and script should print only the differences to third file.

Example:

File1 :

1
2
3
4

File 2 :

1
3
4

Difference :

2

Thanks in adavance
Kumar jeevan
5 REPLIES 5
Matti_Kurkela
Honored Contributor

Re: Need to find the difference of two files and redirects to a third file

Quick & dirty:

cat file1 file2 | sort | uniq -u > file3

If you don't mind a bit of extra information about the differences, *and* want the differences in exact order, then

diff file1 file2 > file3

With the diff command and your example files, file3 would contain:

2d1
< 2

Meaning: "2" exists in file1 but not in file2 ("<" points to file1), and it would be on the 2nd line in file1 ("2d1" indicates the line numbers on file1 and file2, respectively).

MK
MK
Kumar_143
New Member

Re: Need to find the difference of two files and redirects to a third file

Thanks for quick reply.

I just need to apply a Left outer join with First file containg only one column with the second file also containing the same column to fetch the codes present in First file but not in the second file)

Third file should contain only the codes present in First file but not in the second file
Ivan Krastev
Honored Contributor

Re: Need to find the difference of two files and redirects to a third file

You can use grep as well:

grep -v -f File2 File1
2


regards,
ivan

Dennis Handly
Acclaimed Contributor

Re: Need to find the difference of two files and redirects to a third file

>Third file should contain only the codes present in first file but not in the second file

Are the files sorted? If so, you can just use comm(1):
comm -13 file1 file2 > file3

Does the second file have the same number of columns?

>Ivan: You can use grep as well:
>grep -v -f File2 File1

With a million records, this will give terrible performance. Sorting both files and then doing comm(1) will beat that.
anilsingh
Occasional Contributor

Re: Need to find the difference of two files and redirects to a third file

You can use:
sdiff file1 file2 >>output