sed 将双引号 csv 逗号分隔转换为非引号管道分隔

sed convert double quoted csv comma delimited to non quoted pipe delimited

输入文件'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/"([^"])//g; s/,/|/g'  ./input.file

objective第一个换人

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

将解析由 " 划定的任意字段并将它们复制到输出和第二个替换

s/,/|/g

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

$ 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 然后删除所有 "


进一步阅读: