sed,替换匹配的组

时间:2016-08-05 11:09:14

标签: regex sed

我正在尝试仅替换匹配组中的字符

输入:

Foo("test-me");

寻找以下输出:

Foo(TEST_ME);

以下命令捕获引号之间的文本并将其设置为大写

sed 's/Foo("\([^"]*\)");/Foo(\U\1);/' 

=>

Foo(TEST-ME);

错过了

s/-/_/g

1 个答案:

答案 0 :(得分:2)

您需要一个条件循环来替换括号之间的任意数量的连字符:

sed 's/Foo("\([^"]*\)");/Foo(\U\1);/;:a;s/\(Foo([^)-]*\)-/\1_/;ta;'

细节:

:a;  # define the label "a"
s/\(Foo([^)-]*\)-/\1_/; # replace the first hyphen
ta;  # if something is replaced, go to label "a"