Linux
如何從csv中刪除除特定欄位之外的所有雙引號
我想從我的 csv 中刪除所有雙引號,但不是第四個欄位(因為這四個欄位代表文件的 PATH)
請建議如何通過sed或awk或perl 一個班輪等來實現這一點
我現在所知道的是使用簡單的 sed 命令:
sed s"/\"//g" file.csv | sed 's/ //g'
但是這個命令沒有那麼優雅,也適用於第四個欄位(第四個欄位不應該被編輯)
備註 - 還需要刪除引號之間的空格以接近字元
範例(文件 csv 之前)
"24 ","COsc ","LINUX","/VP/Ame/AR/Celts/COf"," fbsutamante ",fbu2012,"kkk","&^#$@J ",,,,, 25,COsc,LINUX,"/VP/Ame/AR/Celts/COf","fbsutamante ",fbu2012,"iiii "," *****",,,,,
範例(文件 csv after after )
24,COsc,LINUX,"/VP/Ame/AR HR/Ce lts/COf",fbsutamante,fbu2012,kkk,&^#$@J,,,,, 25,COsc,LINUX,"/VP/Ame/AR HR/Ce lts/COf",fbsutamante,fbu2012,iiii,*****,,,,,
這可以是一種方式:
awk 'BEGIN{FS=OFS=","} # set input and output field separator as comma {for (i=5; i<=NF; i++) { # loop from 5th field gsub("\"","", $i); # remove " gsub(/^[ \t]+/,"", $i); # remove leading spaces gsub(/[ \t]+$/,"",$i)} # remove trailing spaces }1' file
刪除前導和尾隨基於寶馬的這個答案:刪除 awk 欄位中的前導和尾隨空格。
測試
$ awk 'BEGIN{FS=OFS=","} {for (i=5; i<=NF; i++) {gsub("\"","", $i); gsub(/^[ \t]+/,"", $i); gsub(/[ \t]+$/,"",$i)}}1' file 24,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,kkk,&^#$@J,,,,, 25,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,iiii,*****,,,,,
如果它還必須清理第 1 到第 3 個欄位,只需添加
if (i!=4)
並循環遍歷所有欄位:$ awk 'BEGIN{FS=OFS=","} {for (i=1; i<=NF; i++) {if (i!=4) {gsub("\"","", $i); gsub(/^[ \t]+/,"", $i); gsub(/[ \t]+$/,"",$i)}}}1' a 24,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,kkk,&^#$@J,,,,, 25,COsc,LINUX,"/VP/Ame/AR/Celts/COf",fbsutamante,fbu2012,iiii,*****,,,,,