I have a CSV file which I need to load to a MySQL table. I rely of identifying the columns end by the ,
character. That's why it is important that the ,
does not appear elsewhere other than as a column separator.
I found some rows which contains a column with ,
inside double quotations. for example a line like this one:
12,"name, brand - something, something",age,sex,,,,"name, brand - something, something, something",,,,,
Needs to be converted to:
12,name; brand - something; something,age,sex,,,,name; brand - something; something; something,,,,,
As you see, I replaced the ,
inside double quotations with ;
so that when I load the file in MySQL the ,
inside double quotations are not considered as a separator as they are not ,
anymore. I also removed the double quotations "
as they are not needed.
I tried to automate this for every row in my CSV file using sed as follows:
sed -e 's/"\*,\*"/"\*;\*"/g' -e 's/"//g' input.csv > output.csv
But the result did not replaced the ,
that come inside the double quotations with ;
. It only removed the double quotations:
12,name, brand - something, something,age,sex,,,,name, brand - something, something, something,,,,,