Compare two csv-files and output the diff

by PatricF at 2013-01-10 00:02:12

Hi,

I have two .csv-files, master.csv and slave.csv, and I need to compare those two and get the difference to output in a file.

The master.csv-file contains the up-to-date entries and slave.csv contains the entries that are "active" and need to be updated.
In master.csv there might have been entries added or removed and I need the diff in two files, lets say master.added.csv and master.removed.csv and in those files there are Only the entries that got added/removed and nothing more.

Is this possible?

Let me explain this in a little more simpler way.


master.csv contains:


Firstname;Lastname;Email
John;Doe;John@example.com
Steve;Andersson;steve@company.com
James;Smith;james@aol.com


slave.csv:

Firstname;Lastname;Email
John;Doe;John@example.com
David;Williams;david@hotmail.com


To in this case I should get these files:

master.added.csv:

Firstname;Lastname;Email
Steve;Andersson;steve@company.com
James;Smith;james@aol.com


master.removed.csv:

Firstname;Lastname;Email
David;Williams;david@hotmail.com


I hope you understand what I’m looking for. Is this possible?

Thanks,
-Patric
by nohandle at 2013-01-10 01:09:51
Yes, you can do it, and if the data are in the same format in both files you can do it quite easily I suppose.
I would try to handle the files as plain text. Loading them in memory and comparing them.
see these posts:
viewtopic.php?f=2&t=923
viewtopic.php?f=2&t=991

If you have any problems implementing this, let me know.
by PatricF at 2013-01-10 01:10:57
Thanks for the quick reply, I will check this out!
by PatricF at 2013-01-10 05:11:25
It worked perfectly, thank you very much!