The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to enable #ifdef macro in the command line of make? princelinux Linux 3 12-29-2007 07:10 AM
how i prepare a c++ code(c code) for implementing my own protocol format amitpansuria High Level Programming 1 09-06-2007 08:09 PM
SSH key code versus server key code Texan Security 1 04-12-2006 08:57 AM
How can you show lines surrounding a search string? robster UNIX for Dummies Questions & Answers 3 06-07-2005 08:33 AM
offset - informix chunk shibz UNIX for Dummies Questions & Answers 1 01-27-2003 07:44 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-09-2007
Registered User
 

Join Date: May 2007
Posts: 3
Red face Surrounding a chunk of code by #ifdef and #endif

Hello the great gurus

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:
Code:
MyFunc(a,b,c);
Should be
Code:
#ifdef JUST_DONT
MyFunc(a,b,c);
#endif
Now how do I do that?

Thank you very very much.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-09-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
perl -i -ne ' if ( /MyFunc\(a,b,c\);/ ) { print "#ifdef JUST_DONT\n$_#endif\n" ; } else { print; } ' file
Reply With Quote
  #3 (permalink)  
Old 05-09-2007
Registered User
 

Join Date: May 2007
Posts: 3
MMmmm, thanks!!! I'll try to make this work for me

Last edited by xxxaxa; 05-09-2007 at 03:56 AM.
Reply With Quote
  #4 (permalink)  
Old 05-09-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
perl -i -ne ' if ( /^MyFunc\(.*\);$/ ) { print "#ifdef JUST_DONT\n$_#endif\n" ; } else { print; } ' file
Reply With Quote
  #5 (permalink)  
Old 05-09-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,543
Code:
awk '/^MyFunc/{
		print "#ifdef JUST_DONT"
		print
		print "#endif"} 
     !/^MyFunc/
' file
Reply With Quote
  #6 (permalink)  
Old 05-09-2007
Registered User
 

Join Date: May 2007
Posts: 3
I'm sorry, one last question... I want to put the #endif thing one line after this function. I.E.

Code:
status = MyFunc(a, b, c);
CheckResults(status);
|
V

Code:
#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 )
Reply With Quote
  #7 (permalink)  
Old 05-09-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
From ghostdog74's solution :
Code:
awk '/^MyFunc/{
		print "#ifdef JUST_DONT"
		print
		getline
		print
		print "#endif"
		next
} 
1
' file
Jean-Pierre.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:09 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0