Измените все, кроме первого ',' на «» для каждой строки в файле (bash) [дубликат]

-2
30.08.2018, 19:39
3 ответа

Использование GNUsed:

sed 's/,/<COMMA>/2g' infile

Или портативность:

sed 's/,/<COMMA>/g; s/<COMMA>/,/' infile
3
28.01.2020, 05:14

Возможно, вы можете поместить поля в кавычки, что должно указать парсерам 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'
2
28.01.2020, 05:14

Вы также можете сделать это 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
2
28.01.2020, 05:14

Теги

Похожие вопросы