Show Not Valid CSV Lines with Sed

Show Not Valid CSV Lines with Sed

I have an issue with invalid formatted CSV file. First step show lines with invalid lines.

$ sed -n '/"[^",]*"[^",]*"[^",]*",/,1p' <fileName>

Then I googled a way to replace symbol inside quotes. And I read the next manual http://sed.sourceforge.net/sed1line.txt. So I created a sed script with the next content, called it script.sed:

s/\",\"/\$XXXX\$/g;
:a
s/\([^,]\)"\([^,]\)/\1'\2/g
ta
s/\$XXXX\$/\",\"/g;

invalid-csv.sed view raw

Next we just do:

$ sed -f script.sed <fileName>

And we get a normal csv format file in the output. Next we just add the argument to apply that in this file.

$ sed -i .bak -f script.sed <fileName>