通过bash在单个CSV字段中替换额外的双引号

时间:2016-08-05 10:46:27

标签: python bash csv

我需要替换CSV日志文件中的额外双引号(字段以逗号分隔并用双引号引用)。替换必须仅影响CSV的最后一个字段。

输入日志文件示例:

"24-12-2015","23:07:08","00","01","00","00","START","00","END","JS786JGDG7899JSGJHG"dsdajasghsahd"

预期产出:

"24-12-2015","23:07:08","00","01","00","00","START","00","END","JS786JGDG7899JSGJHG''dsdajasghsahd"

我需要在bash或python中完成这项任务。

2 个答案:

答案 0 :(得分:1)

您可以使用awk:

awk "{gsub(/'/, \"''\", \$NF)} 1" file.csv

"24-12-2015","23:07:08","00","01","00","00","START","00","END","JS786JGDG7899JSGJHG''dsdajasghsahd"

答案 1 :(得分:0)

试试这个;

awk -F, -v OFS="," '{gsub(/\"/,"\47\47",$NF)gsub(/^\47\47/,"\"",$NF);gsub(/\047\047$/,"\"",$NF) }1' file

"24-12-2015","23:07:08","00","01","00","00","START","00","END","JS786JGDG7899JSGJHG''dsdajasghsahd"