Maybe you can put quotes around the fields, which should tell csv parsers that the commas inside are not field separators: sed 's/"/""/g; # escape existing " as "" s/[[:space:]]*,[[:space:]]*/","/; # replace the first , and the # whitespace around it with "," s/^[[:space:]]*/"/; # add a " at the start (and # get rid of whitespace there) s/[[:space:]]*$/"/; # same at the end'