sed转换双引号csv逗号分隔到非引用管道分隔

时间:2016-10-07 03:23:22

标签: csv sed double-quotes

输入文件'input.file'是:

"col one",,,"col, two",,"col, three"
,"col one",,"col, two",,"col, three"
,,"col one","col, two",,"col, three"

所需的输出文件是:

col one|||col, two||col, three
|col one||col, two||col, three
||col one|col, two||col, three

到目前为止,这是现有的:

sed -r 's/"([^"])/\1/g; s/,/|/g'  ./input.file

目标是第一次替换

s/"([^"])/\1/g

将解析由“描述的任意字段”并将它们复制到输出和第二次替换

s/,/|/g

将使用“|”替换双引号字段中未包含的','。

1 个答案:

答案 0 :(得分:1)

$ cat ip.txt 
"col one",,,"col, two",,"col, three"
,"col one",,"col, two",,"col, three"
,,"col one","col, two",,"col, three"

$ perl -pe 's/"[^"]+"(*SKIP)(*F)|,/|/g; s/"//g' ip.txt
col one|||col, two||col, three
|col one||col, two||col, three
||col one|col, two||col, three
  • "[^"]+"(*SKIP)(*F)跳过模式"[^"]+"并查找提供的任何其他替代匹配
    • (*F)(*FAIL)的简短格式,(?!)也可以使用
  • |,备用模式以匹配,
  • |/g将所有,替换为|
  • s/"//g然后删除所有"


进一步阅读: