trax
trax

Reputation: 739

sed, replace on a matched group

I am trying to replace a character on a matched group only

Input:

Foo("test-me");

Looking for the following output:

Foo(TEST_ME);

The below command catch the text between quotes and set it uppercase

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

=>

Foo(TEST-ME);

Just missing the

s/-/_/g

Upvotes: 1

Views: 778

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You need a conditional loop to replace any number of hyphens between the parenthesis:

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

details:

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

Upvotes: 3

Related Questions