Raw Input Desired Output
line 1
line 2 PAT
line 3
line 4 PAT
line 5
line 6
line 7 PAT
line 8
line 9 PAT
line 10
line 11
line 10
line 11
Script and Comments
Script1
[ 1] 1,/PAT/d
[ 2] :loop
[ 3] $q
[ 4] N
[ 5] /PAT/d
[ 6] b loop
Comments
  1. The Pattern Space is abbreviated to `PS'.
  2. Step [1] is used to delete lines from the beginning of a file up to and including the first line containing PAT.
  3. Steps [2] thru [6] constitute a loop, which will
    • keep appending lines to PS Space until a line containing PAT is found.
    • when a line containing PAT is found, command `d' of Step [5] will delete the contents of PS and start a new cycle, then repeat the same procedure as described above.
    • note that step [3] will print the contents of PS then terminate sed when the last line of a file is reached.
Script2
[ 1] 1,/PAT/d
[ 2] :loop
[ 3] N
[ 4] /PAT/d
[ 5] b loop
Comments
  1. When performs command `N' after reading the last line of a file, GNU sed will print the contents of PS then terminate. Using this feature, Step [3] of the last script can be removed.