code_junkie

parse csv file using gawk

Nov 12th, 2011
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. one,two,"three, four",five
  2. "six, seven",eight,"nine"
  3.  
  4. BEGIN { FS="," }
  5. {
  6. for (i=1; i<=NF; i++) printf "field #%d: %sn", i, $(i)
  7. printf "---------------------------n"
  8. }
  9.  
  10. field #1: one
  11. field #2: two
  12. field #3: "three
  13. field #4: four"
  14. field #5: five
  15. ---------------------------
  16. field #1: "six
  17. field #2: seven"
  18. field #3: eight
  19. field #4: "nine"
  20. ---------------------------
  21.  
  22. field #1: one
  23. field #2: two
  24. field #3: "three, four"
  25. field #4: five
  26. ---------------------------
  27. field #1: "six, seven"
  28. field #2: eight
  29. field #3: "nine"
  30. ---------------------------
  31.  
  32. # csv2delim.awk converts comma delimited files with optional quotes to delim separated file
  33. # delim can be any characer, defaults to tab
  34. # assumes no repl characters in text, any delim in line converts to repl
  35. # repl can be any characer, defaults to ~
  36. # changes two consecutive quotes within quotes to '
  37.  
  38. # usage: gawk -f csv2delim.awk [-v delim=d] [-v repl=`"] input-file > output-file
  39. # -v delim delimiter, defaults to tab
  40. # -v repl relacement char, defaults to ~
  41.  
  42. # e.g. gawk -v delim=; -v repl=` -f csv2delim.awk test.csv > test.txt
  43.  
  44. # abe 2-28-7
  45. # abe 8-8-8 1.0 fixed empty fields, added replacment option
  46. # abe 8-27-8 1.1 used split
  47. # abe 8-27-8 1.2 inline rpl and "" = '
  48. # abe 8-27-8 1.3 revert to 1.0 as it is much faster, split most of the time
  49. # abe 8-29-8 1.4 better message if delim present
  50.  
  51. BEGIN {
  52. if (delim == "") delim = "t"
  53. if (repl == "") repl = "~"
  54. print "csv2delim.awk v.m 1.4 run at " strftime() > "/dev/stderr" ###########################################
  55. }
  56.  
  57. {
  58. #if ($0 ~ repl) {
  59. # print "Replacment character " repl " is on line " FNR ":" lineIn ";" > "/dev/stderr"
  60. #}
  61. if ($0 ~ delim) {
  62. print "Temp delimitor character " delim " is on line " FNR ":" lineIn ";" > "/dev/stderr"
  63. print " replaced by " repl > "/dev/stderr"
  64. }
  65. gsub(delim, repl)
  66.  
  67. $0 = gensub(/([^,])""/, "\1'", "g")
  68. # $0 = gensub(/""([^,])/, "'\1", "g") # not needed above covers all cases
  69.  
  70. out = ""
  71. #for (i = 1; i <= length($0); i++)
  72. n = length($0)
  73. for (i = 1; i <= n; i++)
  74. if ((ch = substr($0, i, 1)) == """)
  75. inString = (inString) ? 0 : 1 # toggle inString
  76. else
  77. out = out ((ch == "," && ! inString) ? delim : ch)
  78. print out
  79. }
  80.  
  81. END {
  82. print NR " records processed from " FILENAME " at " strftime() > "/dev/stderr"
  83. }
  84.  
  85. "first","second","third"
  86. "fir,st","second","third"
  87. "first","sec""ond","third"
  88. " first ",sec ond,"third"
  89. "first" , "second","th ird"
  90. "first","sec;ond","third"
  91. "first","second","th;ird"
  92. 1,2,3
  93. ,2,3
  94. 1,2,
  95. ,2,
  96. 1,,2
  97. 1,"2",3
  98. "1",2,"3"
  99. "1",,"3"
  100. 1,"",3
  101. "","",""
  102. "","""aiyn","oh"""
  103. """","""",""""
  104. 11,2~2,3
  105.  
  106. rem test csv2delim
  107. rem default is: -v delim={tab} -v repl=~
  108. gawk -f csv2delim.awk test.csv > test.txt
  109. gawk -v delim=; -f csv2delim.awk test.csv > testd.txt
  110. gawk -v delim=; -v repl=` -f csv2delim.awk test.csv > testdr.txt
  111. gawk -v repl=` -f csv2delim.awk test.csv > testr.txt
  112.  
  113. BEGIN { FS="," }
  114. {
  115. for (i=1; i<=NF; i++) {
  116. f[++n] = $i
  117. if (substr(f[n],1,1)==""") {
  118. while (substr(f[n], length(f[n]))!=""" || substr(f[n], length(f[n])-1, 1)=="\") {
  119. f[n] = sprintf("%s,%s", f[n], $(++i))
  120. }
  121. }
  122. }
  123. for (i=1; i<=n; i++) printf "field #%d: %sn", i, f[i]
  124. print "----------------------------------n"
  125. }
Add Comment
Please, Sign In to add comment