Unix sed命令-全局替换不起作用

mgdq6dx1  于 2022-09-21  发布在  Unix
关注(0)|答案(1)|浏览(223)

我有一个场景,我们希望将数据之间的多个双引号替换为单引号,但由于输入数据用"comma"分隔符分隔,所有列数据都用双引号括起来,""遇到了问题,解释如下:

示例数据如下所示:

"int","","123","abd"""sf123","top"

因此,输出将是:

"int","","123","abd"sf123","top"

已尝试以下方法以获得解决方案,但只有第一次出现才起作用,不确定问题是什么??

sed -ie 's/,"",/,"NULL",/g;s/""/"/g;s/,"NULL",/,"",/g' inputfile.txt

1.将所有->From,“”,替换为“Null”,
1.将->的所有多次出现从“”或“”或“”改为“”(单次出现)
1.将1步更改替换回原始->From,“Null”,To,“”,

但是,只有第一个示例正在更改,其余的内容如下所示:

如果输入为:

"int","","","123","abd"""sf123","top"

输出结果如下:

"int","","NULL","123","abd"sf123","top"

但是,输出应该是:

"int","","","123","abd"sf123","top"
oogrdqng

oogrdqng1#

您可以尝试这款perl,但要提前看一下:

perl -pe 's/("")+(?=")//g' file

"int","","123","abd"sf123","top"
"int","","","123","abd"sf123","top"
"123"abcs"

其中,输入为:

cat file

"int","","123","abd"""sf123","top"
"int","","","123","abd"""sf123","top"
"123"""""abcs"

分手:

  • ("")+:匹配1+双引号
  • (?="):如果这些对后面跟一个"

相关问题