Skip to content

Instantly share code, notes, and snippets.

@creativetechnologist
Created May 31, 2013 17:21
Show Gist options
  • Save creativetechnologist/5686479 to your computer and use it in GitHub Desktop.
Save creativetechnologist/5686479 to your computer and use it in GitHub Desktop.
Script for Ben to process email files
# Script for Ben to process email files
# By: Richard G Smith
# Running notes
# 1. Copy this script into the folder containing the input csv file, eg. vinciparser.rb
# 2. Change the two constants, INPUT_FILE_NAME and OUTPUT_FILE_NAME to the desired values
# 3. At the command prompt type ruby vinciparser.rb
# 4. All going well you should have a new file in your folder named as per OUTPUT_FILE_NAME
# Requirements to run the script
# Ensure input file is UFT-8 encoded
# Ruby is insalled on your system and you have the csv gem available, I'd suggest ruby version 1.9.3
require 'csv'
# Change these to the values you require
INPUT_FILE_NAME = "input.csv"
OUTPUT_FILE_NAME = "output.csv"
firstrow = true
counter = 0
CSV.open(OUTPUT_FILE_NAME, "wb") do |csv|
CSV.foreach(INPUT_FILE_NAME) do |row|
firstname = row[0].split(" ")[0]
surname = row[0].split(" ")[1]
# Ok, should do this with a regexp but this makes it more readable, and adaptable
if row[3] # Check there is an email address, just in case
start_parenth = row[3].index("(")
end_parenth = row[3].index(")")
email = row[3][start_parenth+1..end_parenth-1] if start_parenth && end_parenth
else
email = ""
end
puts "Writing Row for #{email}"
csv << ["firstname", "surname", "display name", "email"] if firstrow
csv << [firstname, surname, "#{firstname} #{surname}", email] unless firstrow
firstrow = false
counter += 1 unless firstrow
end
end
puts "Process complete, #{counter} records written to #{OUTPUT_FILE_NAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment