Raw Input
Line 1 ....
Line 2 ========= print this
Line 3 HERE .... print this
Line 4 ========= print this
Line 5 ....
Line 6 ....
Line 7 ========= print this
Line 8 HERE .... print this
Line 9 HERE .... print this
Line 0 ========= print this
Line 1 ....
Line 2 ....
Desired Output
Line 2 ========= print this
Line 3 HERE .... print this
Line 4 ========= print this
Line 7 ========= print this
Line 8 HERE .... print this
Line 9 HERE .... print this
Line 0 ========= print this
Script and Comments
Script1
[ 1] /PAT/{
[ 2] :loop
[ 3] n
[ 4] /PAT/b loop
[ 5] b
[ 6] }
[ 7] $d
[ 8] N
[ 9] /PAT/b loop
[10] D
Comments
  1. Description:
    • If line N of the datafile contains PAT, this script will print line N-1, N, and N+1.
    • If line N and N+1 both contains PAT, this script will print line N-1, N, N+1, and N+2, without printing line N and N+1 twice.
  2. Flowchart: