Raw Input Desired Output
ghi line 1
abc line 2
abc line 3
xyz line 4
abc line 5
xyz line 6
ghi line 7
abc line 8
ghi line 1
abc line 2
abc line 3
==========
xyz line 4
abc line 5
==========
xyz line 6
ghi line 7
abc line 8
Script and Comments
Script1
[ 1] /^abc/!b
[ 2] $!N
[ 3] s/\nxyz/\n==========&/
[ 4] t
[ 5] P
[ 6] D
Comments
  1. In Step [1], if a line begins with 'abc' then proceed to Step [2]; otherwise, command 'b' make sed jump to the end of script, print that line and start a new cycle.
  2. If the line in question begins with 'abc', command 'N' of Step [2] will be used to append the next line to Pattern Space (later abbreviated to PS), separating them by a newline character.
  3. Now, there are two lines in PS,
    • if the second one begins with 'xyz', command 's' of Step [4] will succeed, then command 't'(Step [4]) make sed jump to the end of script, print contents of PS, and start a new cycle;
    • otherwise, command 'P'(Step [5]) will print the first line, then command 'D'(Step [6]) will remove it from PS and make sed jump to Step [1].