I'm writing a bash script that aims to impact all the files in a folder, in order to replace all calls to a given C function by changing the argument. Here is what the current calls look like, and what I want to obtain :
myfunc( foo ) ----> myfunc( foo, foo+1 )
I use the following find/sed command and it does the job:
find . -name "*" -print | xargs sed -i 's/myfunc\([^)]*)\)/myfunc((\1),(\1)+1)/g'
But the issue arise when I have to deal with instructions/arguments that contains parentheses, like in the following instruction:
sqrt( myfunc( (foo+foo)*bar ) );
What happens is that the first closing parenthesis (i.e. the one after foo
) is interpreted as the end of the myfunc
argument, which it is not...
How can I get a regexp (or any other command) that capture and replace (foo+foo)*bar
, i.e. exactly the content of the correct level of parentheses, whatever the number of parentheses levels ?