我有一个txt文件,我只需要在引号之间用空格替换逗号。
例如:
This,is,example,"need,delete comma",xxxx
结果应该是:
This,is,example,"need delete comma",xxxx
我有这个命令,但这是错的:
sed -i '/^"/,/^"/s/.,/ /' output.txt
答案 0 :(得分:2)
试试这个:
awk'NR%2-1 {gsub(/,/,“”)} 1'RS = \“ORS = \”input.txt> output.txt的
输入:
这是一个例子,“需要,删除逗号”,xxxx
输出:
这是一个例子,“需要删除逗号”,xxxx
答案 1 :(得分: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""
答案 2 :(得分:0)
以下awk如何,我在寻找#34; ...&#34;之间的匹配然后只需删除该匹配中的所有逗号。然后用旧的&#34; ..&#34;替换它的新值。值。
awk '{match($0,/\".*\"/);val=substr($0,RSTART,RLENGTH);gsub(/,/," ",val);gsub(/\".*\"/,val,$0)} 1' Input_file
EDIT1:看到RomanPerekhrest的输入文件后,上面的代码略有变化,禁止更改&#34;,&#34;在任何一行。
awk '{match($0,/\".*\"/);val=substr($0,RSTART,RLENGTH);gsub(/[^","],/," ",val);gsub(/\".*\"/,val,$0)} 1' Input_file
答案 3 :(得分:0)
echo'这是,例如,“需要,删除逗号”,xxxx'| awk -F \“'{sub(/,/,”“,$ 2); print}'OFS = \”
这是一个例子,“需要删除逗号”,xxxx