Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement