Marjer
Marjer

Reputation: 1403

Removing double quotes in a CSV with delimeter inside

awk -F , '{if ($2 ~ /^".*"$/) $2 = substr($2, 2, length($2)-2); {gsub(/""/, "", $2);} print $2;}' sample.csv

I have devloped the above awk to remove double quotes in 2nd column. the awk was working fine for the below content

sample.csv

file,"file2",file3

output of sample.csv with above awk

file,file2,file3

But the awk is not working for the below scenario

sample1.csv

file,"fil,e2",file3

output of sample1.txt with above awk

file,"fil,e2",file3

Required output of sample1.txt

file,file2,file3

I know the issue is because of comma separator inside the Double quotes, is there any way to skip this?

I want to remove double quotes on 2nd column(the source may contain multiple comma(,) inside the double quotes like "fi,l,e2)

Upvotes: 0

Views: 356

Answers (1)

Ed Morton
Ed Morton

Reputation: 203209

$ cat file
file,"file2",file3,"a,b,c","d,e"

$ awk -F'"' -v OFS= '{for (i=2;i<=NF;i+=2) gsub(/,/,"",$i)}1' file
file,file2,file3,abc,de

Upvotes: 5

Related Questions