Использование GNUsed
:
sed 's/,/<COMMA>/2g' infile
Или портативность:
sed 's/,/<COMMA>/g; s/<COMMA>/,/' infile
Возможно, вы можете поместить поля в кавычки, что должно указать парсерам csv, что запятые внутри не являются разделителями полей:
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'
Вы также можете сделать это POSIX-ly следующим образом:
sed -e '
y/,/\n/ ;# change all commas to newlines, which are guaranteed to not be there
s/\n/,/ ;# then change the first of those newlines to a comma, i.e., restore
s//<COMMA>/g ;# and all the remaining newline(s) change to <COMMA>
' dat.csv