输入文件'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
将使用“|”替换双引号字段中未包含的','。
答案 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
然后删除所有"
进一步阅读: