Linux

CSV - 在缺少的欄位周圍添加引號

  • September 19, 2019

我的 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","",""

我認為一個簡單的sed命令就可以了:

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

輸出(如果有前導逗號):

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

第一個表達式處理行首的逗號;第二個表達式處理行尾的逗號;第三個表達式處理中間的缺失欄位。

引用自:https://unix.stackexchange.com/questions/542695