Created
February 13, 2016 22:31
-
-
Save lygaret/7b32ecabc57f1dfe5184 to your computer and use it in GitHub Desktop.
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
#!/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 |
Author
lygaret
commented
Feb 13, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment