Skip to content

Instantly share code, notes, and snippets.

@pkrnjevic
Created April 13, 2012 05:04
Show Gist options
  • Save pkrnjevic/2373838 to your computer and use it in GitHub Desktop.
Save pkrnjevic/2373838 to your computer and use it in GitHub Desktop.
Similar to intersect.pl but in ruby. This version just reads files "file1 file2 file3".
#!/usr/bin/env ruby
require 'digest/sha1'
# Build an array of hashes for each file, using sha1 of line as key
def get_digests_for_file(file_name)
h = {}
File.readlines(file_name).each do |line|
h[Digest::SHA1::digest line] = line
end
h
end
h1 = get_digests_for_file("file1")
h2 = get_digests_for_file("file2")
h3 = get_digests_for_file("file3")
# Using set intersections, build a array of keys found in all 3 files
keys_in_all_files = h1.keys & h2.keys & h3.keys
# Print the found lines
keys_in_all_files.each {|k| puts h1[k]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment