Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline delete with sed

Tags:

sed

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!

like image 619
lyricat Avatar asked Jan 05 '11 19:01

lyricat


People also ask

How to delete lines using sed?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

Can sed replace multiple lines?

By using N and D commands, sed can apply regular expressions on multiple lines (that is, multiple lines are stored in the pattern space, and the regular expression works on it): $ cat two-cities-dup2.

How do you put multiple patterns in sed?

Use multiple patterns at once with SedLook out for the semicolon(;) to add more patterns within the single command.

How do I delete a range of lines in Linux?

The sed command can remove the lines of any range. For this, we just have to enter 'minimum' and 'maximum' line numbers. In this example, we will remove the lines ranging from 4 to 7 numbers. After removing these ranges of lines, our file will look like this.


1 Answers

You can try sed -e '/^#ifdef _WIN32/,/^#endif/d' but it does not generalize to more complex cases of nesting.

like image 92
Ben Jackson Avatar answered Oct 24 '22 23:10

Ben Jackson