CSV - 在缺失字段周围添加引号

CSV - 在缺失字段周围添加引号

我的 CSV 文件如下所示 -

1,2,3,4,5,6,7,8
"AK","87","86","81","83","78",,

我如何将其转变为这样 -

1,2,3,4,5,6,7,8
"AK","87","86","81","83","78","",""

答案1

我认为一个简单的sed命令就可以了:

sed -e 's/^,/"",/' -e 's/,$/,""/' -e 's/,,/,"",/g' file

输出(如果有前导逗号):

1,2,3,4,5,6,7,8
"","AK","87","86","81","83","78","",""

第一个表达式处理行开头的一个逗号;第二个表达式处理行尾的一个逗号;第三个表达式处理中间缺失的字段。

相关内容