gpt4 book ai didi

regex - 多次出现捕获组的 sed 正则表达式

转载 作者:行者123 更新时间:2023-12-02 08:02:26 27 4
gpt4 key购买 nike

我有一个包含多行的 SQL 文件,如下所示:

INSERT INTO mytable VALUES (1,2,3,'foo'),(2,4,5,'bar'),(3,5,9,'baz'),...;
INSERT INTO mytable VALUES (10,2,3,'foo'),(11,4,5,'bar'),(12,5,9,'baz'),...;

我想在每一行的每个元组中添加第五列,就像这样

INSERT INTO mytable VALUES (1,2,3,'foo','txt_foo'),(2,4,5,'bar','txt_bar'),(3,5,9,'baz','txt_baz'),...;
INSERT INTO mytable VALUES (10,2,3,'baz','txt_baz'),(11,4,5,'bar', 'txt_bar'),(12,5,9,'foo','txt_foo'),...;

完成任务的 sed 或 awk 命令是什么?我尝试了多个正则表达式组合,但它似乎没有选择任何事件或它选择了整行。

我试过,例如:

sed -E "s/\((d+),(d+),(d+),'(.*)'\)/(\1,\2,\3,'\4','txt_\4')/g" /myfile.sql

它什么也不做。

最佳答案

你可以使用这个sed:

sed -E "s/'([^']+)'\)/'\1', 'txt_\1')/g" file

INSERT INTO mytable VALUES (1,2,3,'foo', 'txt_foo'),(2,4,5,'bar', 'txt_bar'),(3,5,9,'baz', 'txt_baz'),...;
INSERT INTO mytable VALUES (10,2,3,'foo', 'txt_foo'),(11,4,5,'bar', 'txt_bar'),(12,5,9,'baz', 'txt_baz'),...;

关于regex - 多次出现捕获组的 sed 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55637800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com