Skip to content

Instantly share code, notes, and snippets.

@lygaret
Created February 13, 2016 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lygaret/7b32ecabc57f1dfe5184 to your computer and use it in GitHub Desktop.
Save lygaret/7b32ecabc57f1dfe5184 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
STOP_QUOTED_RE = /(?:[^"]|"{2})(",)/
STOP_NORMAL_RE = /(,)/
# returns [chunk, rest], rest is nil at the end
def read_chunk(line)
quoted = line[0] == '"'
start = quoted ? 1 : 0
stop_re = quoted ? STOP_QUOTED_RE : STOP_NORMAL_RE
stop_match = stop_re.match line
# no match means end of line or badly formatted
return [line, nil] if stop_match.nil?
stop = stop_match.offset(1)[0] - 1
nstart = stop_match.offset(1)[1]
# otherwise it's thte start to match begin, match end to end
chunk = line[start..stop]
rest = line[nstart..-1]
[chunk.gsub(/""/, '"'), rest]
end
# returns an array of chunks for a line
def read_line(line)
results = []
until line.nil?
chunk, line = read_chunk(line)
results << chunk
end
results
end
$stdin.each_line do |line|
row = read_line(line)
puts row.join("|")
end
@lygaret
Copy link
Author

lygaret commented Feb 13, 2016

## ruby csv_parser.rb
"Alexander ""Alex""",Mendoza,"San Francisco",CA,4
Alexander "Alex"|Mendoza|San Francisco|CA|4
Chris,Hitchens,Denver,CO,40
Chris|Hitchens|Denver|CO|40
"""""Alex"""" Ander",Jones,"""Twin Cities""",MN,5
""Alex"" Ander|Jones|"Twin Cities"|MN|5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment