2017-09-08 61 views
0

我有一个txt文件,我只需要在引号之间用空格替换逗号。在两个字符之间替换逗号

例如:

This,is,example,"need,delete comma",xxxx 

而且结果应该是:

This,is,example,"need delete comma",xxxx 

我有这样的命令,但它是错误的:

sed -i '/^"/,/^"/s/.,/ /' output.txt 
+1

我很好奇,为什么要替换逗号?你是否想要解决没有适当的CSV解析器? –

回答

2

试试这个:

AWK“NR%2-1 {GSUB(/,/, “ ”)} 1' RS = \“ ORS = \” input.txt中> output.txt的

输入:

这是,例如, “需要,删除逗号”,XXXX

输出:

这是,例如, “需要删除逗号”,XXXX

+1

这会在多行输出结尾添加末尾的'''',你可以从'testfile'的回答中进行测试https://stackoverflow.com/questions/46118785/replace-comma-between-two-characters/46119331 #46119331 – RomanPerekhrest

0

复杂AWK溶液:

testfile样品:

This,is,example,"need,delete comma",xxxx 
asda 
asd.dasd,asd"sdf,dd","sdf,sdfsdf" 
"some,text,here" another text there"" 

作业:

awk -F'"' '$0~/"/ && NF>1{ for(i=1;i<=NF;i++) { if(!(i%2)) gsub(/,/," ",$i) }}1' OFS='"' testfile 

输出:

This,is,example,"need delete comma",xxxx 
asda 
asd.dasd,asd"sdf dd","sdf sdfsdf" 
"some text here" another text there"" 
0

下面的awk如何查找“...”之间的匹配,然后简单地删除该匹配中的所有逗号。然后用旧的“..”值替换它的新值。

awk '{match($0,/\".*\"/);val=substr($0,RSTART,RLENGTH);gsub(/,/," ",val);gsub(/\".*\"/,val,$0)} 1' Input_file 

EDIT1:在任何一行看到RomanPerekhrest的INPUT_FILE在上面的代码,这将禁止更改变化不大“”后。

awk '{match($0,/\".*\"/);val=substr($0,RSTART,RLENGTH);gsub(/[^","],/," ",val);gsub(/\".*\"/,val,$0)} 1' Input_file 
0

回声 '这是,例如, “需要,删除逗号”,XXXX' | AWK -F \”“{子(/,/,””,$ 2);打印}” OFS = \ “

这是例如”需要删除逗号“,xxxx

相关问题