Issue
How can I use sed to duplicate part of a string?
hello foo(ignore this);
hello bar(or that);
hello func(or anything really);
with
hello foo(x) foo(y)
hello bar(x) bar(y)
hello func(x) func(y)
I know I can use & multiple times in the replace statement of sed but I have trouble having the matching pattern & be only what's between hello and (
Solution
I think you're on the right track with &
:
sed 's/(.*/(/; s/[^ ]*(/&x) &y)/' test.txt
- Clear out everything after open paren:
s/(.*/(/
- Then capture function name and open paren as
&
and repeat withx)
andy)
:s/[^ ]*(/&x) &y)/
[^ ]*
is capturing the string of non-space chars before the open paren
Answered By - stevesliva Answer Checked By - Clifford M. (WPSolving Volunteer)