Raw Input Desired Output
Line -9
Line -8
Line -7
Line -6
Line -5
Line -4
Line -3
Line -2
Line -1
Line -6
Line -5
Line -4
Line -3
Line -2
Line -1
Description
The following scripts use the Pattern Space (abbreviated to `PS' later) to implement a `sliding windows':
  • The first 6 lines of a file will fill that window.
  • Then we slide it by deleting the first line and joining the next line of the input file to the end of it. This procedure repeats until the last line of the file is reached.
Script and Comments
Script1
[ 1] :loop
[ 2] $q
[ 3] /^\([^\n]*\n\)\{5\}/D
[ 4] N
[ 5] b loop
Comments
  1. Steps [3] and [4] are used to slide the window:
    • command `D' of Step [3] deletes the first line of PS.
    • sed then branches to Step [1].
    • Steps [2] and [3] are not performed since the conditions are not satisfied.
    • Step [4] joins the next line to the end of PS.
Script2
[ 1] :loop
[ 2] N
[ 3] /^\([^\n]*\n\)\{5\}/!b loop
[ 4] $!D
Comments
  1. The loop consists of Steps [1] thru [3] repeats until PS has 6 lines.
  2. Then Steps [4] and [2] are used to slide the window:
    • command `D' of Step [4] deletes the first line of PS.
    • sed then branches to Step [1].
    • Step [2] joins the next line to the end of PS.
    • Step [3] are not performed since the condition is not satisfied.