Question

I want to call a trace/log function in the start of all the function in all files under given folder. An example,

from

void getVal(int a) 
{
   return mArray[a];
}

To

void getVal(int a) 
{pr_debug();
   return mArray[a];
}

So, i tried as below, but did not work. find soc -type f -exec sed -i 's/)\n{\n/)\n{pr_debug()\n/g' {} \;

I searched and tried other ways, but no luck.

find soc  -exec sed -i 's/)\
> {\
> /)\
> {pr_debug();\
> /g' {} \;


find soc -type f -exec sed -i "s/)\\`echo -e '\n\r'`{\\`echo -e '\n\r'`/)\\`echo -e '\n\r'`{pr_debug()\\`echo -e '\n\r'`/g" {} \;

Can anyone help me?

Was it helpful?

Solution

You can use this sed,

sed 'N; s/)[ ]*\n{/&\npr_debug();/g'

(OR)

sed '/(.*)[ ]*$/{N; s/)[ ]*\n[ ]*{/&\npr_debug();/g}'

Explanation:

N                  - will read next line and append
s/pattern/replace/ - normal substitution takes place
&                  - will replace with what exactly matched in pattern

EDIT: the find command:

find . -type f -exec sed -i '/(.*)[ ]*$/{N; s/)[ ]*\n[ ]*{/&\npr_debug();/g}' {} \;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top