我想从 csv 中删除所有双引号,但不是第四个字段(因为这四个字段代表文件的路径)
请建议如何实现这一点sed或者awk或者Perl 1 衬垫, ETC
我现在所知道的是使用简单的 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 文件)
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,*****,,,,,
答案1
这可以是一种方式:
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,*****,,,,,
如果还必须清理第一个到第三个字段,只需添加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,*****,,,,,
答案2
只需将它们全部删除然后放回去即可:
sed 's/"//g;s/,/,"/3;s/,/",/4'