Raw Input Desired Output
1
2
3 HERE
4
5 HERE
6
7 HERE
8
1
2
4
5 HERE
6
7 HERE
8
Script and Comments
Script1
[ 1] /PAT/{
[ 2] $d
[ 3] N
[ 4] s/.*\n//
[ 5] :loop
[ 6] n
[ 7] b loop
[ 8] }
Comments
  1. It's the first time /PAT/{...} is executed when the first line containing PAT is read.
  2. If this line is also the last line of the file, just delete it.
  3. Otherwise, we
    • append next line by step [3]
    • removing the first part in Pattern Space(this is the first line containing /PAT/) by step [4]
    • begin the loop(step [5]-[7]): "read next line then print it out"
    .