title: How would I find similar lines in two CSV files?
body: <p>Here is my code but it takes forever for huge files:</p>
  
  <pre><code>require 'rubygems'
  require "faster_csv"
  
  fname1 =ARGV[0]
  fname2 =ARGV[1]
  if ARGV.size!=2
      puts "Display common lines in the two files \n Usage : ruby user_in_both_files.rb &lt;file1&gt; &lt;file2&gt; "
      exit 0
  end
  
  puts "loading the CSV files ..."
  file1=FasterCSV.read(fname1, :headers =&gt; :first_row)
  file2=FasterCSV.read(fname2, :headers =&gt; :first_row)
  puts "CSV files loaded"
  
  #puts file2[219808].to_s.strip.gsub(/\s+/,'')
  
  lineN1=0
  lineN2=0
  # count how many common lines
  similarLines=0
  file1.each do |line1|
      lineN1=lineN1+1
      #compare line 1 to all line from file 2
      lineN2=0
      file2.each do |line2|
          puts "file1:l#{lineN1}|file2:l#{lineN2}"
          lineN2=lineN2+1
          if ( line1.to_s.strip.gsub(/\s+/,'') == line2.to_s.strip.gsub(/\s+/,'') ) 
              puts "file1:l#{line1}|file2:l#{line2}-&gt;#{line1}\n"
              similarLines=similarLines+1
          end
      end 
  end
  puts "#{similarLines} similar lines."
  </code></pre>