Reputation: 1996
I'm trying to use sed to delete all occurrences of
#ifdef _WIN32
#endif
Where all that exists between #ifdef and #endif is an empty line. I have limited experience using sed, I've read some documentation on the multi line features but I can't seem to figure it out. Any help is appreciated!
Upvotes: 5
Views: 3403
Reputation: 753665
For this job, I'd recommend using a tool designed for the job - rather than sed
.
Use coan; it has a mode for editing #ifdef
and #ifndef
and #if
and #elsif
lines selectively. For example, you'd use:
coan source -U_WIN32 sourcefile.c
This would deal with all _WIN32
sections, leaving behind only what was necessary.
See also my related question: Is there a C pre-processor which eliminates #ifdef blocks based on values defined/undefined?
Upvotes: 4
Reputation: 140307
If there exists pairs of #ifdef _WIN32/#endif
that have non-empty lines between them that you don't want to delete, then use the following:
sed 'N;N;s/\n#ifdef _WIN32\n[[:space:]]*\n#endif\n/\n/;P;D'
this is the first line
#ifdef _WIN32
// Don't delete this comment!
#endif
stuff here
more stuff here
#ifdef _WIN32
#endif
last line
$ sed 'N;N;s/\n#ifdef _WIN32\n[[:space:]]*\n#endif\n/\n/;P;D' ifdef.sed
this is the first line
#ifdef _WIN32
// Don't delete this comment!
#endif
stuff here
more stuff here
last line
Upvotes: 2
Reputation: 93710
You can try sed -e '/^#ifdef _WIN32/,/^#endif/d'
but it does not generalize to more complex cases of nesting.
Upvotes: 5