Skip to content

Instantly share code, notes, and snippets.

@vsushkov
Created January 8, 2012 12:16
Show Gist options
  • Select an option

  • Save vsushkov/1578162 to your computer and use it in GitHub Desktop.

Select an option

Save vsushkov/1578162 to your computer and use it in GitHub Desktop.
flatprocessor
#!/usr/bin/env ruby
file = File.open(ARGV[0], 'r')
lines = Array.new
file.each do |line|
lines << line
end
phones = Array.new
lines.each do |line|
line.gsub(/\d+-\d+-\d+-\d+-\d+/).each do |phone|
phones << phone
end
end
duplicated_phones = Hash.new(0)
phones.each do |phone|
duplicated_phones[phone] += 1
end
duplicated_phones.reject! {|phone, occurances| occurances == 1}
result = Array.new
lines.each do |line|
line_contains_duplicated_phone = false
duplicated_phones.each do |phone, occurances|
line_contains_duplicated_phone = true if line.match(phone)
end
result << line unless line_contains_duplicated_phone
end
new_filename = 'clean_' + ARGV[0]
new_file = File.open(new_filename, 'w')
result.each{|result_line| new_file.print result_line}
puts 'Result file is ' + new_filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment