sed正则表达式用于捕获组的多次出现

时间:2019-04-11 17:05:00

标签: regex awk sed

我有一个包含多行的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

它什么都不做。

2 个答案:

答案 0 :(得分:5)

您可以使用此while

<form name="input" method="Post" id="aerForm" name="aerForm" action="https://geico.com">
    <input type="hidden" name="rs:command" id="rs:command" value="test"/>
    <input type="hidden" name="rs:Format" id="rs:Format" value="PDF"/>
    <input type="hidden" name="rc:Toolbar" id="rc:Toolbar" value="false"/>
    <input type="submit" value="Get your total">
</form>

sed

答案 1 :(得分:1)

您也可以尝试Perl。这里$ q以\ x27

十六进制形式存储单引号
$ cat antonio.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')

$ perl -pe ' $q="\x27"; s/$q(.+?)$q\)/$q$1$q,${q}txt_$1$q\)/g  ' antonio.sql
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')

$
相关问题