Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- one,two,"three, four",five
- "six, seven",eight,"nine"
- BEGIN { FS="," }
- {
- for (i=1; i<=NF; i++) printf "field #%d: %sn", i, $(i)
- printf "---------------------------n"
- }
- field #1: one
- field #2: two
- field #3: "three
- field #4: four"
- field #5: five
- ---------------------------
- field #1: "six
- field #2: seven"
- field #3: eight
- field #4: "nine"
- ---------------------------
- field #1: one
- field #2: two
- field #3: "three, four"
- field #4: five
- ---------------------------
- field #1: "six, seven"
- field #2: eight
- field #3: "nine"
- ---------------------------
- # csv2delim.awk converts comma delimited files with optional quotes to delim separated file
- # delim can be any characer, defaults to tab
- # assumes no repl characters in text, any delim in line converts to repl
- # repl can be any characer, defaults to ~
- # changes two consecutive quotes within quotes to '
- # usage: gawk -f csv2delim.awk [-v delim=d] [-v repl=`"] input-file > output-file
- # -v delim delimiter, defaults to tab
- # -v repl relacement char, defaults to ~
- # e.g. gawk -v delim=; -v repl=` -f csv2delim.awk test.csv > test.txt
- # abe 2-28-7
- # abe 8-8-8 1.0 fixed empty fields, added replacment option
- # abe 8-27-8 1.1 used split
- # abe 8-27-8 1.2 inline rpl and "" = '
- # abe 8-27-8 1.3 revert to 1.0 as it is much faster, split most of the time
- # abe 8-29-8 1.4 better message if delim present
- BEGIN {
- if (delim == "") delim = "t"
- if (repl == "") repl = "~"
- print "csv2delim.awk v.m 1.4 run at " strftime() > "/dev/stderr" ###########################################
- }
- {
- #if ($0 ~ repl) {
- # print "Replacment character " repl " is on line " FNR ":" lineIn ";" > "/dev/stderr"
- #}
- if ($0 ~ delim) {
- print "Temp delimitor character " delim " is on line " FNR ":" lineIn ";" > "/dev/stderr"
- print " replaced by " repl > "/dev/stderr"
- }
- gsub(delim, repl)
- $0 = gensub(/([^,])""/, "\1'", "g")
- # $0 = gensub(/""([^,])/, "'\1", "g") # not needed above covers all cases
- out = ""
- #for (i = 1; i <= length($0); i++)
- n = length($0)
- for (i = 1; i <= n; i++)
- if ((ch = substr($0, i, 1)) == """)
- inString = (inString) ? 0 : 1 # toggle inString
- else
- out = out ((ch == "," && ! inString) ? delim : ch)
- print out
- }
- END {
- print NR " records processed from " FILENAME " at " strftime() > "/dev/stderr"
- }
- "first","second","third"
- "fir,st","second","third"
- "first","sec""ond","third"
- " first ",sec ond,"third"
- "first" , "second","th ird"
- "first","sec;ond","third"
- "first","second","th;ird"
- 1,2,3
- ,2,3
- 1,2,
- ,2,
- 1,,2
- 1,"2",3
- "1",2,"3"
- "1",,"3"
- 1,"",3
- "","",""
- "","""aiyn","oh"""
- """","""",""""
- 11,2~2,3
- rem test csv2delim
- rem default is: -v delim={tab} -v repl=~
- gawk -f csv2delim.awk test.csv > test.txt
- gawk -v delim=; -f csv2delim.awk test.csv > testd.txt
- gawk -v delim=; -v repl=` -f csv2delim.awk test.csv > testdr.txt
- gawk -v repl=` -f csv2delim.awk test.csv > testr.txt
- BEGIN { FS="," }
- {
- for (i=1; i<=NF; i++) {
- f[++n] = $i
- if (substr(f[n],1,1)==""") {
- while (substr(f[n], length(f[n]))!=""" || substr(f[n], length(f[n])-1, 1)=="\") {
- f[n] = sprintf("%s,%s", f[n], $(++i))
- }
- }
- }
- for (i=1; i<=n; i++) printf "field #%d: %sn", i, f[i]
- print "----------------------------------n"
- }
Add Comment
Please, Sign In to add comment