Search code examples
bashcsvawksedex

How to replace quotes inside a quoted field of a non-standard CSV file using a one-liner bash command?


I have a file like this:

col1×col2×col3
12×"Some field with "quotes" inside it"×"Some field without quotes inside but with new lines \n"

And I would like to replace the interior double quotes with single quotes so the result will look like this:

col1×col2×col3
12×"Some field with 'quotes' inside it"×"Some field without quotes inside but with new lines \n"

I guess this can be done with sed, awk or ex but I haven't been able to figure out a clean and quick way of doing it. Real CSV files are of the order of millions of lines.

The preferred solution would be a one-liner using the aforementioned programs.


Solution

  • A simple workaround using sed, based on your fields separator ×, could be:

     sed -E "s/([^×])\"([^×])/\1'\2/g" file
    

    This replace each " which is preceded and followed by any characters other that ×, with '.

    Note that sed not support positive lookahead, so we have to group and reinsert the patterns.