I need to write a shell script which will input a string in my .c files after a quite complicated pattern to match.
The pattern is: )\n{
,
without any tabs/spaces between the \n
and the {
. That's to say that I want to match with a {
located in the first column of my file (this in order to ONLY match with )\n{
following function declarations in C, not the ones following loops or conditions).
void some_function_declaration(char var1, char var2)
{
I have read manuals, forums and still cannot figure the right way to write the pattern to match or find the corresponding regex. The corresponding output would be:
void some_function_declaration(char var1, char var2)
{ time_exe(__func__, cl(clock()));
rest of the function...
What follows is what I've came up with so far and doesn't work.
Try 1
sed -i '' '/)$\n{/a time_exe(__func__, cl(clock()));' >> list_func2.c
Try 2
sed -i '' '/)\n{ /a time_exe(__func__, cl(clock()));' >> list_func2.c
Try 3
sed -i '' '/)/\n{/a time_exe(__func__, cl(clock()));' >> list_func2.c
I would be very glad to hear your recommandations about this matter.
Perl can be good for this:
$ cat file.c
void some_function_declaration(char var1, char var2)
{
if (a)
{ b; }
}
void func2()
{
//
}
$ perl -0777 -pe 's {\)\n\{} {$& some other stuff;\n}g' file.c
void some_function_declaration(char var1, char var2)
{ some other stuff;
if (a)
{ b; }
}
void func2()
{ some other stuff;
//
}
Same caveat's as Aaron's answer due to reading the whole file into memory.