Search code examples
linuxbashsed

Using sed to replace a character but not if it is in a string in bash csv file


Good morning everyone, I have a csv and want to replace all delimiter "," with the delimiter ";", but I have to maintain the "," if it is in a string (in csv field):

Current csv

"001","User,Super","04/04/2024"

Expected csv

"001";"User,Super";"04/04/2024"

Is there a way to exclude the check if it is between a string? I would like to do this in bash with a couple of commands without the need of writing a custom loop to check every character and check the position

I tried the following command but with no success:

sed -r 's/("*,*")|,/\;/g' test.csv

Solution

  • This might work for you (GNU sed):

    sed -E ':a;s/^(("[^,"]*",)*"[^,"]*),/\1\n/;ta;y/\n,/,;/' file
    

    Iterate along the line replacing any commas within double quotes by newlines.

    When there are no more substitutions, translate newlines to commas and commas to semicolons.