Raw Input Desired Output
1 PAT print this
2
3 --- print this
4 PAT print this
5 PAT print this
6
7 --- print this
8 PAT print this
9 PAT print this
10 
1 PAT print this
3 --- print this
4 PAT print this
5 PAT print this
7 --- print this
8 PAT print this
9 PAT print this
Script and Comments
Script1
[ 1] $!N
[ 2] /PAT/P
[ 3] D
Comments
  1. A line must be printed if it contains PAT or the next one contains PAT.
  2. The Pattern Space is abbreviated to `PS'.
  3. If the current line is not the last one of the datafile, Step [1] will append the next line to PS.
  4. After Step [1], PS have two lines in it. The regular expression PAT matches PS if
    • the first line of PS contains PAT, or
    • the second line of PS contains PAT, or
    • both of them contain PAT.
    In these cases, the first line of PS must be printed. That's why command `P' must be performed if PAT matches PS.
  5. Command `D' of Step [3] deletes the first line of PS and then makes sed jump to Step [1].