regex 在CSV值的双引号中搜索和替换(转义)双引号

368yc8dk  于 2022-12-30  发布在  其他
关注(0)|答案(2)|浏览(243)

我想用'''(三个单引号)替换所有出现在,"",之间的"。这将在一个csv文件中完成,并在所有可能的嵌套引号中完成,以免格式混乱。
例如
"test","""","test"变为
"test","''''''","test".
另一个例子:
"test","quotes "inside" quotes","test"
变成
"test","quotes '''inside''' quotes".
我使用https://sed.js.org/来测试替换。
我现在拥有的是

sed "s/\([^,]\)\(\"\)\(.\)/\\1'\\''\\3/g"

但它似乎还没有完成,也没有涵盖我想要的所有情况。
例如,工程:
"anything","inside "quotes"","anything"-〉
"anything","inside '''quotes'''","anything"
不适用于:
"anything","inside "test" quotes","anything"-〉
x1米11米1x
预期-〉
"anything","inside '''test''' quotes","anything"
也许有人擅长正则表达式,可以帮助?

e0bqpujr

e0bqpujr1#

使用sed

$ cat input_file
"test","""","test"
"test","quotes "inside" quotes","test"
"anything","inside "quotes"","anything"
"anything","inside "test" quotes","anything"

$ sed -E ':a;s/(,"[^,]*('"'"'+)?)"([^,]*"(,|$))/\1'"'''"'\3/;ta' input_file
"test","''''''","test"
"test","quotes '''inside''' quotes","test"
"anything","inside '''quotes'''","anything"
"anything","inside '''test''' quotes","anything"
falq053o

falq053o2#

使用变量${qs}可以避免转义三个单引号。
开始将所有引号替换为${qs}
接下来,在行开始、行结束和,附近重置替换。

qs="'''"
sed "s/\"/${qs}/g; s/^${qs}/\"/; s/${qs}$/\"/; s/${qs},${qs}/\",\"/g" csvfile

相关问题