sed

Does anyone know how I can do the following?

I have multiple blocks of text, I enclose an example of 2.
1 has the word DRAFT in the 2nd line and will always be in the 2nd line.
How can I remove all blocks of text that have DRAFT in them?

sedl

SAMPLE SOME TEXT AND MORE TEXT 
DRAFT
SAMPLE TEXT
SAMPLE TEXT
BLOCK ONE
END

SAMPLE SOME TEXT AND MORE TEXT 
SAMPLE TEXT
SAMPLE TEXT
BLOCK TWO
END

Many thanks...

Hi,
you can try this if you use gnu sed:

sed -n ':w;/END/bc;$bc;N;bw;:c;/^[^\n]*\nDRAFT/!p' file

Regards.

Try also

awk '!($2 ~ /DRAFT/)' FS="\n" RS= file

A Unix sed needs the labels at the end of a line (semicolon not allowed there)

sed -e ':w' -e '/END/bc' -e '$bc' -e 'N;bw' -e ':c' -e '/\nDRAFT/d' file

This version allows DRAFT on any line within the block.

I think you would also need to set ORS otherwise it will remove all the empty lines:

awk '$2!~/DRAFT/' RS= FS='\n' ORS='\n\n' file
1 Like