Это наивная реализация.Вы можете улучшить его.
Так же, как идея для вас.
Вот пример csv-файлов:
$ cat first.csv
name,email,phone,job,created_at
John,john@john.us,112,police,21.02.
Jack,jack@jack.us,112,ambulance,22.02.
Ivan,ivan@ivan.ru,02,kgb,23.02.
$ cat second.csv
email,note
ivan@ivan.ru,some note
Наивный сценарий:
require 'csv'
first_csv = CSV.
read('first.csv', headers: true).
map { |value| { name: value['name'],
email: value['email'],
phone: value['phone'],
job: value['job'],
created_at: value['created_at'] } }
second_csv = CSV.
read('second.csv', headers: true).
map { |value| { email: value['email'],
note: value['note'] } }
# The same email searching
first_csv.each do |f|
second_csv.each do |s|
f.merge! s if f[:email] == s[:email]
end
end
# Write to new CSV
CSV.open('new.csv', 'w') do |csv|
csv << %w(name email phone job created_at note)
first_csv.each do |info|
csv << info.values_at(:name, :email, :phone, :job, :created_at, :note)
end
end
Проверка
$ cat new.csv
name,email,phone,job,created_at,note
John,john@john.us,112,police,21.02.,
Jack,jack@jack.us,112,ambulance,22.02.,
Ivan,ivan@ivan.ru,02,kgb,23.02.,some note