Created
May 29, 2012 09:33
-
-
Save commuterjoy/2823543 to your computer and use it in GitHub Desktop.
Extract details from a weird data format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Written because I couldn't bear the thought of my mother cutting a pasting | |
# four hundred key-value pairs from a text file in to Excel. | |
# Usage: ruby extract.rb < data | |
class Person | |
attr_accessor :full_name, :first_name, :other_names, :name_tokens, :company, :email | |
def first_name | |
full_name.split(' ').first if full_name | |
end | |
def other_names | |
if full_name | |
if a = full_name.split(' ') | |
return a.slice!(1..a.length) if a.is_a? Array | |
end | |
end | |
end | |
def complete? | |
(self.instance_variables === [:@full_name, :@company, :@email]) | |
end | |
def to_tsv | |
puts [full_name, first_name, other_names, company, email].join("\t") | |
end | |
end | |
p = nil | |
STDIN.read.scan(/\[(\w+)\] => (.+)?$/).each_with_index do |a, index| | |
p = Person.new if p == nil | |
if p.complete? | |
puts p.to_tsv | |
p = nil | |
end | |
p.full_name = a[1] if a.first == 'name' | |
p.company = a[1] if a.first == 'company' | |
p.email = a[1] if a.first == 'email' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment