Surrounding a chunk of code by #ifdef and #endif

Hello the great gurus :slight_smile:

I'm quite new to this, so perhaps I'm asking a simple and trivial question (which is good, because you'll answer for sure :))))

Anyway. I have an amount of *.c files (about 100), and what I want to do, is to surround a specific function call with #ifdef and #endif.

Example:

MyFunc(a,b,c);

Should be

#ifdef JUST_DONT
MyFunc(a,b,c);
#endif

Now how do I do that?

Thank you very very much. :o

perl -i -ne ' if ( /MyFunc\(a,b,c\);/ ) { print "#ifdef JUST_DONT\n$_#endif\n" ; } else { print; } ' file

MMmmm, thanks!!! I'll try to make this work for me :slight_smile:

perl -i -ne ' if ( /^MyFunc\(.*\);$/ ) { print "#ifdef JUST_DONT\n$_#endif\n" ; } else { print; } ' file
awk '/^MyFunc/{
		print "#ifdef JUST_DONT"
		print
		print "#endif"} 
     !/^MyFunc/
' file

I'm sorry, one last question... I want to put the #endif thing one line after this function. I.E.

status = MyFunc(a, b, c);
CheckResults(status);

|
V

#ifdef JUST_DONT
status = MyFunc(a, b, c);
CheckResults(status);
#endif

How do I "insert" this extra newline into the search string? (I'm terribly sorry for my dumb questions, but I really didn't succeed to use the online help :frowning: )

From ghostdog74's solution :

awk '/^MyFunc/{
		print "#ifdef JUST_DONT"
		print
		getline
		print
		print "#endif"
		next
} 
1
' file

Jean-Pierre.