sed将双引号csv逗号分隔转换为非引号管道分隔
输入文件“input.file”是:sed将双引号csv逗号分隔转换为非引号管道分隔,csv,sed,double-quotes,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
"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
将双引号字段中的“,”替换为“|”
$ 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
跳过模式“[^”]+”(*跳过)(*F)
并查找提供的任何其他替代匹配项“[^”]+”
是(*F)
的缩写,也可以使用(*FAIL)
(?!)
要匹配的备选模式|,
,
将所有|/g
替换为,
|
然后删除所有s///g
“
进一步阅读:
perl-pe的s/“[^”+”/$&=~s | pattern | replace | gr/ge'ip.txt
。。。这是在所有带引号的字符串中执行的,它正在将模式
更改为替换
。。。因此,对于您的情况,replace
将为空,pattern
将是您需要删除的任何控制字符