fork download
  1. BEGIN { while (getline str > 0) printfields(str) }
  2.  
  3. function printfields(str) {
  4. for (k=1; k<=csvsplit(str,arr); k++)
  5. print k, arr[k] }
  6.  
  7. function csvsplit(str, arr, i,j,n,s,fs,qt) {
  8. # split comma-separated fields into arr
  9. # return number of fields in arr
  10. # in Europe, change fs from "," to ";"
  11. # fields surrounded by double-quotes may
  12. # contain commas; doubled double-quotes
  13. # represent a single embedded quote
  14. delete arr
  15. s = "START"; n = 0; fs = ","; qt = "\""
  16. for (i = 1; i <= length(str); i++) {
  17. if (s == "START") {
  18. if (substr(str,i,1) == fs) {
  19. arr[++n] = "" }
  20. else if (substr(str,i,1) == qt) {
  21. j = i+1; s = "INQUOTES" }
  22. else { j = i; s = "INFIELD" } }
  23. else if (s == "INFIELD") {
  24. if (substr(str,i,1) == fs) {
  25. arr[++n] = substr(str,j,i-j)
  26. j = 0; s = "START" } }
  27. else if (s == "INQUOTES") {
  28. if (substr(str,i,1) == qt) {
  29. s = "MAYBEDOUBLE" } }
  30. else if (s == "MAYBEDOUBLE") {
  31. if (substr(str,i,1) == fs) {
  32. arr[++n] = substr(str,j,i-j-1)
  33. gsub(qt qt, qt, arr[n])
  34. j = 0; s = "START" } } }
  35. if (s == "INFIELD" || s == "INQUOTES") {
  36. arr[++n] = substr(str,j) }
  37. else if (s == "MAYBEDOUBLE") {
  38. arr[++n] = substr(str,j,length(str)-j)
  39. gsub(qt qt, qt, arr[n]) }
  40. else if (s == "START") { arr[++n] = "" }
  41. return n }
Success #stdin #stdout 0s 4548KB
stdin
abc,123,"de""fgh",456,"ij,klm"
"ij,klm","de""fgh"
stdout
1 abc
2 123
3 de"fgh
4 456
5 ij,klm
1 ij,klm
2 de"fgh