in reply to Diff 2 files and print output to file
A typical approach would be to use a hash for such tasks. For example:
Read in file 1 and put every item into a hash (as key — e.g. $hash{$item} = $linenumber;)
Next, read file 2 and check for every item if it's found in the hash (if (exists $hash{$item}) ...). If found, write it to the file which will contain the common items, and set the value to zero ($hash{$item} = 0;) to mark it being common. Otherwise, write it to the file holding the items only found in file 2.
Lastly, write the items in the hash with values other than zero into the file holding the items only found in file 1. (Depending on whether you need the latter items to be in the original order, you might want to sort them by line number, or go through file 1 another time, or some such.)
(The details may vary slightly depending on whether the same item can occur multiple times in one or both input files...)
|
|---|