我有一个包含以下数据的 csv 文件
输入
"01","5","Male","[""No.**"",""**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
我想替换方括号之间的“,”字符串,但不替换方括号之外的数据。
输出:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""
我尝试了下面的命令,但它确实替换了方括号之外的数据,但不适用于所有行。
sed '/\[/,/\]/s/"",""/,/'
答案1
sed '/\[/,/\]/s/"",""/,/'
这将查找带有 a 的行[
,然后查找带有 a 的行]
,并在这些行之间替换每行中遇到的第一个"",""
。它并没有真正关注它们[..]
在一行中的位置。
作为零阶尝试,如下所示:
$ cat x
"01","5","Male","[""No.**"",""**kg""]","","",""
$ sed 's/\(\[.*\)"",""\(.*\]\)/\1,\2/g' x
"01","5","Male","[""No.**,**kg""]","","",""
该模式匹配 a [
、任何内容"",""
、任何内容和 a ]
,同时捕获除 the 之外的所有内容,"",""
以便它可以将各个部分重新组合在一起。
这会破坏诸如[..],"","",[..]
(括号在"",""
看到 a 之前关闭,模式搜索后面的]
)和[.."","".."",""..]
(括号内有多个"",""
序列,仅删除一个)之类的内容。
对于 Perl 来说更通用一些,尽管这是一个可怕的替换中的替换技巧。您可能应该使用适当的解析器来代替:
$ cat y
no removal here: [...],"","",[...]
double removal here: [ "","" "","" ]
[""remove"",""here""],""not"",""here"",[""also"",""here""]
$ perl -pe 'sub x {$a = shift; $a =~ s/"",""/,/g; return $a;}
s/(\[.*?\])/ x($1) /eg ' y
no removal here: [...],"","",[...]
double removal here: [ , , ]
[""remove,here""],""not"",""here"",[""also,here""]
(.*?
是非贪婪匹配,它会尽快停止,即]
在本例中是第一次停止。)
答案2
sed '/"Male/s/"",""/,/1' filename
输出:
"01","5","Male","[""No.**,**kg""]","","",""
"02","6","FeMale","[""No.""]","","",""