谢谢大家的时间。
我试图用一个双引号替换两个双引号。
例如,我有以下数据:
1,2,3,4,"abc""
我希望它是:
1,2,3,4,"abc"
这是我正在使用的awk命令,但它不起作用:
cat sample.csv | awk '{ sub(/"\*""/, "\*", $5)}'
提前感谢您的帮助!
答案 0 :(得分:3)
使用sed:
将所有""
替换为"
sed 's/""/"/g' file
答案 1 :(得分:1)
使用sed
,只需删除最后一个"
:
sed 's/"$//' sample.csv
awk
使用sub()
的类似逻辑:
awk 'sub("\"$", "")' sample.csv
示例:强>
% sed 's/"$//' <<<'1,2,3,4,"abc""'
1,2,3,4,"abc"
% awk 'sub("\"$", "")' <<<'1,2,3,4,"abc""'
1,2,3,4,"abc"
答案 2 :(得分:0)
如果你喜欢冒险,你可以试试下面的东西。这平衡了文本周围的引号:
$ cat 39316179
1,2,3,4,"abc""
1,2,""4",3,"""abc""" # Notice irregular quoting.
1,2,4,"3"",""""abc""""
$ sed -E 's/"+([^"]*)"+/"\1"/g' 39316179
1,2,3,4,"abc"
1,2,"4",3,"abc"
1,2,4,"3","abc"
<强>解释强>
"+
表示查找双引号一次或多次([^"]*)
表示查找not double quotes
([^"]
)任意次(*
)。周围的()
表示我们希望重新使用此后匹配的模式。()
存储的部分和结束双引号。答案 3 :(得分:0)
试试这个:
sed 's/"\+/"/g'
+
表示给定角色的出现次数超过1次
我为你的例子做了一个更大的混乱只是为了表明它适用于给定角色的任意数量的连续出现,在这种情况下"
原始档案:
$ cat d.txt
1,2,3,4,"abc""
"1"",""2""","""3"","4",""abc""
""""1"","""2"",""""3"","""4"","abc""
"""1"""",""2"","""3""",""""4"""",""""abc"""""
结果:
$ cat d.txt | sed 's/"\+/"/g'
1,2,3,4,"abc"
"1","2","3","4","abc"
"1","2","3","4","abc"
"1","2","3","4","abc"
答案 4 :(得分:0)
echo '1,2,3,4,"abc""' | awk '{sub(/c""/,"c\"")}1'
1,2,3,4,"abc"
它做什么?它删除了最后一个引号。也许更好的方法是这样说:
awk'sub(/“$ /,”“)”然后我们不必费心去逃避任何事情。