Raw Input Desired Output
...
...
PAT-1
...
PAT-2
...
PAT-3
...
...
final
...
...
final
Script and Comments
Script1
[ 1] 1,/PAT/d
[ 2] $q
[ 3] N
[ 4] /\n.*PAT/d
Comments
  1. The Pattern Space is abbreviated to PS.
  2. Step [1] is used to delete lines from the first line of a file till the first one containing PAT.
  3. After the first line containing PAT,
    • we keep appending lines to PS via Step [3].
    • If the latest appended line contains PAT,
      Step [4] is used to discard lines kept in PS so far since they are not desired; sed then branches to Step [1], where command `d' will no longer be executed, then [2].
    • Those desired lines will be printed by Step [2] once the last line of a file is reached.