Skip to content

Instantly share code, notes, and snippets.

@thejefflarson
Created January 26, 2012 17:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejefflarson/1684042 to your computer and use it in GitHub Desktop.
Save thejefflarson/1684042 to your computer and use it in GitHub Desktop.
require 'iconv'
def shift
line = ''
in_quote = 0
loop do
if read_line = $stdin.gets("\n")
line += read_line.chomp
else
return nil
end
csv = []
current = ''
line.split(',', -1).each do |cell|
cell = Iconv.iconv('ascii//translit//ignore', 'utf-8', cell).first
if cell.count('"').zero? && current.empty?
csv << cell.squeeze(' ').strip
next
end
in_quote += cell.count '"'
current += cell
if in_quote % 2 == 0
csv << current.gsub(/(\n|"|\r|,)/, '').dup.squeeze(' ').strip
current = ''
end
end
if in_quote % 2 == 0
break csv
end
end
end
csv = []
while !csv.nil?
csv = shift
$stdout.puts(csv.join ",") unless csv.nil?
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment