Raw Input Desired Output
1
2 ======
3  PAT
4 ======
5
6 ======
7  PAT
8  PAT
9 ======
10
2 ======
3  PAT
4 ======
6 ======
7  PAT
8  PAT
9 ======
Script and Comments
Script1
[ 1] $!{
[ 2] N
[ 3] /\n.*PAT/{
[ 4] P
[ 5] D
[ 6] }
[ 7] }
[ 8] /PAT/b
[ 9] D
Comments
  1. The flow chart used to construct this script is as follows:
  2. The gray path in the flow chart may be used to replace the red one, rendering a neat script without sacrificing the correctness.
  3. The next script is an equivalent but neat one.
Script2
[ 1] $!N
[ 2] /\n.*PAT/!{
[ 3] P
[ 4] D
[ 5] }
[ 6] /PAT/b
[ 7] D
Comments