I have two text files and I want to compare them and save the matched columns to a new text file.
file1:
114.74721
114.85107
114.85107
2.96667
306.61756
file2:
115.06603 0.00294 5.90000
114.74721 0.00674 5.40000
114.85107 0.00453 6.20000
111.17744 0.00421 5.50000
192.77787 0.03080 3.20000
189.70226 0.01120 5.00000
0.46762 0.00883 3.70000
2.21539 0.01290 3.50000
2.96667 0.01000 3.60000
5.43310 0.00393 5.50000
0.28537 0.00497 5.10000
308.82348 0.00183 6.60000
306.61756 0.00359 5.20000
And I want the output to be
114.74721 0.00674 5.40000
114.85107 0.00453 6.20000
114.85107 0.00453 6.20000
2.96667 0.01000 3.60000
306.61756 0.00359 5.20000
I used a script but the output file is empty, Could you help me?
note: some rows in file1.txt have the same value
>>> textfile = file('results.txt', 'wt')
>>> file1 = open("file1.txt", "r")
>>> file2 = open("file2.txt", "r")
>>> file3 = open("results.txt", "a")
>>> list1 = file1.readlines()
>>> list2 = file2.readlines()
>>> file3.write("The following entries appear in both lists: \n")
>>> for i in list1:
... for j in list2:
... if i==j:
... file3.write(i)