declare a hash (e.g. "my %lines." open the first input file while reading the first input one line at a time use the line as a hash key and assign "1" as the hash value close the file open output.file1 to hold "matching lines" open output.file2 to hold "distinct lines" open the second input file while reading the second input one line at a time if the current line exists as a hash key and the hash value is "1" print this line to the "matching lines" file increment the hash value otherwise print this line to the "distinct lines" file (maybe include the file name) having read all input, now loop over all the keys of the hash if the hash value assigned to this key is still "1" print this hash key to the "distinct lines" file (maybe include the file name)