Created
May 13, 2018 11:04
-
-
Save AoJ/4e8b32e69c8619a5c71a515f86a06f50 to your computer and use it in GitHub Desktop.
awk csv parser :)
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
#!/usr/bin/awk -f | |
# Sample 1: 3141,"Smith, John",$52000 | |
# col[0] = 3141 | |
# col[1] = Smith, John | |
# col[2] = $52000 | |
# Sample 2: 3141,"John ""The Rocket"" Smith",$52000 | |
# col[0] = 3141 | |
# col[1] = John "The Rocket" Smith | |
# col[2] = $52000 | |
head = $0 { | |
delete col | |
n = 0 | |
while (match(head, /^("[^"]*"(,|$)|[^,]*(,|$))/)) { | |
item = substr(head, 1, RLENGTH) | |
sub(/,$/, "", item) | |
if (item ~ /^"/) item = substr(item, 2, length(item) - 2) | |
gsub(/""/, "\"", item) # "" -> " | |
gsub(/\\"/, "\"", item) # \" -> " | |
col[n++] = item | |
head = substr(head, RLENGTH + 1) | |
if (length(head) == 0) break | |
} | |
for (i = 0; i < n; i++) printf "col[%u] = %s\n", i, col[i] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment