How to search and replace a particular code line in C source using Perl/anything else?
I have a C source code spread over many source files (*.c). For porting reasons say I need to comment out below statements from all source files
fprintf(stderr,"......",....);
The problem is these fprintf开发者_StackOverflows could be multiline statements, meaning broken into two/or more lines spread over two lines in source files with a newline character(carriage returned entered at end of one line).
How can I find such fprintfs scattered across all source files, replace them with a multiline C comment as:
/*
*/
Since they are multiline, the find and replace command of source editors did not help.
I am trying to read the source file using a PERL script but, parse them to do this but could not.
Any pointers would be useful.
thank you. -AD.
What you are looking for is named "coccinelle", it's semantic patch tool for C, via this you can easily do this. viz. http://coccinelle.lip6.fr/
Just
#undef fprintf
#define fprintf(stream, format, ...) 42
at the top of your files and be happy.
For a pure perl solution try something like this:
my $all_lines; { # (limit scope of $/, if appropriate) local $/; #slurp entire file in one go $all_lines = <$file_handle> $all_lines =~ s|(\bfprintf\s*\(\s*stderr\s*,.*?\)\s*;)|/* $1 */|sg; }
精彩评论