Raw Input Desired Output
1
2
3 HERE
4
5 HERE
6
7 HERE
8
1
2
3 HERE
4
5 HERE
6
8
Script and Comments
Script1
[ 1] /PAT/{
[ 2] :loop
[ 3] $d
[ 4] N
[ 5] /PAT[^\n]*$/{
[ 6] h
[ 7] s/\n[^\n]*$//
[ 8] p
[ 9] g
[10] s/.*\n//
[11] b loop
[12] }
[13] $!b loop
[14] s/[^\n]*\n//
[15] }
Comments
  1. Flowchart: