Skip to content

Instantly share code, notes, and snippets.

@jamesmacaulay
Created August 30, 2010 19:28
Show Gist options
  • Save jamesmacaulay/557905 to your computer and use it in GitHub Desktop.
Save jamesmacaulay/557905 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# call with filenames as arguments
# outputs lines which appear in all files
# (stripping leading and trailing whitespace, in no predictable order)
# $ cat > foo
# apple
# orange
# pear
# $ cat > bar
# pear
# orange
# pineapple
# $ cat > baz
# pineapple
# apple
# orange
# $ ./intersection.rb foo bar baz
# orange
require 'set'
lines = {}
ARGF.each_line do |line|
(lines[ARGF.filename] ||= Set.new) << line.strip
end
sets = lines.values
intersection = sets.inject(sets.pop) {|intersection,set| intersection & set}
intersection.each do |line|
puts line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment