Comparing two text files

  • Thread starter Thread starter Holger Kasten
  • Start date Start date
H

Holger Kasten

Hello,

I am looking for a fast and efficient way to compare two text files and
create a thrid one.

E.g.

Input file 1:
Number 1
Number 2
Number 3
Number 4

Input file 2:
Number 2
Number 3
Number 5

The Output should be in case 1 (Show lines that are the same):
Number 2
Number 3

In case 2 (Show different lines):
Number 1
Number 4
Number 5

This should work regarless of the order of the lines!

Any suggestions?

Thx Holger
 
You could read the contents of one file into a hashtable using the value in
the file as the key, then read through the other file and for each value
check to see if the value exists as a key in the hashtable, if it does then
you have a match if not then there is no match and you can put the values
into a DoesMatch and DoesNotMatch collection structure.

Looking up values in a hashtable is very quick. To make it more efficient
you may not read all of the contents into the hashtable at once, you could
page the data, but then the process would take longer and you would have to
do multiple scans.

Hope that helps
Mark R Dawson
 
Hi,

Even better just a counter as the value in the hashtable , and just read
both files, when done those containing a value of 2 appears in both files,
with value 1 appear in one file only.

Playing with the values you could also know what lines appeared in the first
and not in the second or viceversa.

cheers,
 
Back
Top