views:

26

answers:

3

I want to remove all the text between (and including) two strings in all the files in a directory. For an example, the files look something like this:

flag
bla bla bal
bla bla bla
endflag

etc..

This is what I'm doing with sed:

sed -i "s:flag.*endflag::m" *

However, the 'm' option is not part of sed. All the other Stack Overflow threads on this topic resort to using perl, awk, or a sed wrapper to accomplish this. Isn't there a way to tell sed to match newline characters?

+1  A: 

In sed you can specify a range of lines where the range is a pattern marking the start of the range and another pattern marking the end of the range:

sed -i '/flag/,/endflag/d' *
Mark Byers
Hmm, this looks promising, but it's not working. The exact command I'm running is: sed -i ':<form:,:</form>:d' *.aspx *.htm */*.htm
nnyby
Nevermind! It worked when I used slashes instead of colons. thanks!
nnyby
A: 

You have to copy the text into the hold buffer first, than you can match \n like any other character. Normally, there is only one line in the hold buffer at a time, so \n is never part of the analyzed string. The following call first copies the first line into the hold buffer (1h) then appends all following lines into the hold buffer (1!H) and after having added the last line ($) does the replacement (s/).

sed -n '1h;1!H;${;g;s/flag.*endflag\n//g;p;}' foo.txt
fschmitt
+1  A: 

To use alternate delimiters for range specifiers in sed, you have to escape the first one:

sed -i '\:<form:,\:</form>:d' *.aspx *.htm *.html
Dennis Williamson