0

I have a file z.csv and it is csv file. SOme of the rows have double quotes and commas within. I need to remove the commas in the double quotes and then remove the double quotes from each record.


"abc","1,234",90,1

,"1,12,000","12,000",RTC

,,,

qqq,www,222,"323"

I need output like this,

  • Output:
abc,1234,90,1

,112000,12000,RTC

,,,

qqq,www,222,323

Any help would be appreciated in UNIX!!!

1 Answers1

1
$ awk '{
    while ( match($0,/"[^"]*"/) ) {
        fld = substr($0,RSTART+1,RLENGTH-2)
        gsub(/,/,"",fld)
        $0 = substr($0,1,RSTART-1) fld substr($0,RSTART+RLENGTH)
    }
    print
}' file
abc,1234,90,1

,112000,12000,RTC

,,,

qqq,www,222,323

If that's not all you need then see What's the most robust way to efficiently parse CSV using awk?.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185