From: Brian Candler Date: 2011-10-06T00:09:10+09:00 Subject: Re: Best way to compare lines in 2 files Guillaume Bloss wrote in post #1025125: > I have 2 list in 2 separate files > I call them 1st file = old file, 2nd file = new file > They are similar, except there is some lines added and some lines > removed in both files, and I only want to keep : lines common in both > files and line only existing in my 2nd file. You want two separate output files, one containing lines common to both, and one containing lines only in the 2nd file? As long as preserving the order isn't important, look at the manpages for 'sort' and 'join'. Otherwise, since the files aren't too big, you can read all of file1 into a Hash of {linedata=>true}. Then read through file2 and print a line only if the corresponding Hash entry is true. seen = {} File.open("file1") do |f| f.each_line do |line| seen[line] = true end end File.open("file2") do |f| f.each_line do |line| if seen[line] print "In both,#{line}" else print "Only in file 2,#{line}" end end end -- Posted via http://www.ruby-forum.com/.