Skip to content

Instantly share code, notes, and snippets.

@AoJ
Created May 13, 2018 11:04
Show Gist options
  • Save AoJ/4e8b32e69c8619a5c71a515f86a06f50 to your computer and use it in GitHub Desktop.
Save AoJ/4e8b32e69c8619a5c71a515f86a06f50 to your computer and use it in GitHub Desktop.
awk csv parser :)
#!/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