Raw Input Desired Output
Line 1
Line 2 PAT
Line 3
Line 4 
Line 5
Line 6 PAT
Line 7
Line 8
Line 9
Line 10
Line 1
Line 2 PAT
Line 3
Line 4 
Line 5
Script and Comments
Script1
[ 1] /PAT/,$!b
[ 2] /PAT/{
[ 3] x
[ 4] /./p
[ 5] d
[ 6] }
[ 7] H
[ 8] d
Comments
  1. The Pattern Space and the Hold Space are abbreviated to `PS' and `HS', respectively.
  2. Step [1] is used to print lines before the first line containing PAT.
  3. For lines from the first one containing PAT, this script uses the HS to keep them until another line containing PAT is read.
  4. When another line containing PAT is read,
    • Command `x' of Step [3] moves the current line to HS, and the lines kept to PS.
    • Command `p' of Step [4] prints the contents of PS, i.e., those kept lines.
    • Command `d' of Step [5] empties PS and then start a new cycle.
  5. If the current line does not containing PAT, Step [7] appends it to HS.