This question already has an answer here:

I've been using the following code successfully to find 00 PROGRAM and delete all of the following lines of the block, throughout the many blocks in the file, but to keep the last line of the block if it contained XYZ.

sed -n '/00 PROGRAM/,/^$/{/./{h;d}; x;/XYZ/p}' file 

Now I need the opposite. I need to find Group cat, and delete only the following lines of the block until it gets to the next empty/blank line. The initial pattern needs to remain in the file. The blank line must stay in the file. Other lines of text also need to remain in the file. Can anyone suggest changes to this structure to make it work, or suggest a new line of code?

Before code runs:

 Group cat
 Group div
 Group member1
 Group member2
 Group select
      <empty line>
Common cat
Last update

After code runs:

Group cat
     <empty line>
Common cat
Last update

Then it will search for the next pattern match and delete lines in that block and so on.

marked as duplicate by don_crissti, elbarna, Archemar, Timothy Martin, G-Man Mar 15 '18 at 1:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Just to make sure I wasn't dreaming, I tried the file example provided at [link] https://unix.stackexchange.com/questions/88382/using-sed-to-delete-everything-between-two-words with @Wildcard's answer to this question and indeed got the same result. But with my file I get a different result. Sometimes there are leading spaces in front of my test... would that make a different? If anyone want to test it, let me know how I can post a small portion of my file and you can see for yourself. For whatever reason, the @Wildcard answer works consistently. – 985ranch Mar 15 '18 at 20:43

The following produces the desired output:

sed -e '/Group cat/,/^$/{//b' -e '/./d;}' file

On the first Group cat line, the b command will be executed, thus branching to the end of the script and printing that line (the default action).

On all other lines up to the next empty line, the d command will be executed—but not on the empty line itself.

Not the answer you're looking for? Browse other questions tagged or ask your own question.