Sed multiline delete with pattern

I want to remove all multi-line occurrences of a pattern, e.g.

  {START-TAG
  foo bar
  ID: 111
  foo bar
  END-TAG}

  {START-TAG
  foo bar
  ID: 222
  foo bar
  END-TAG}

  {START-TAG
  foo bar
  ID: 333
  foo bar
  END-TAG}

I want to remove all parts between START-TAG and END-TAG that contain specific identifiers.

So, to remove ID: 222, this will remain:

  {START-TAG
  foo bar 2
  ID: 111
  foo bar 3
  END-TAG}


  {START-TAG
  foo bar 2
  ID: 333
  foo bar 3
  END-TAG}

I have a blacklist of identifiers that need to be removed.

I assume this will make a fairly simple multi-line serial expression script. Can anyone help?

This is very similar to the question: sed multiline replace , but not the same.

+4
source share
1 answer

You can use the following:

sed '/{START-TAG/{:a;N;/END-TAG}/!ba};/ID: 222/d' data.txt

Structure:

/{START-TAG/ { # Match '{START-TAG'
:a             # Create label a
N              # Read next line into pattern space
/END-TAG}/!    # If not matching 'END-TAG}'...
           ba  # Then goto a
}              # End /{START-TAG/ block
/ID: 222/d     # If pattern space matched 'ID: 222' then delete it. 
+6
source

All Articles