Raw Input Desired Output
Line 1 AAAA
Line 2 BBBB
Line 3 CCCC
Line 4 DDDD
Line 5 EEEE
Line 6 FFFF
Line 7 GGGG
Line 8 HHHH
Line 9 IIII
Line 1 AAAA
Line 2 BBBB
Line 3 CCCC
Script and Comments
Script1
[ 1] :loop
[ 2] 1,5{
[ 3] $d
[ 4] N
[ 5] b loop
[ 6] }
[ 7] $d
[ 8] N
[ 9] P
[10] D
Comments
  1. 'Pattern space' is abbreviated to 'PS'.
  2. Steps [1] thru [6] constitute a loop. When sed exits this loop, PS will contains Line 1 thru 6 of the processed file. For files containing less than 6 lines, they will be discarded by the command 'd' in step [3] .
  3. When Line 6 of the file has been read, unless the last line is reached, we will each time
    • Append one next line to PS.
    • Print the first line in PS.
    • Remove the first line in PS.
  4. When the last line has been read, PS contains all the last 6 lines of the processed file, all we have to do is discarding them via command 'd' in [7].
  5. A refined version is shown in Script2.
Script2
[ 1] :loop
[ 2] $d
[ 3] N
[ 4] 2,6b loop
[ 5] P
[ 6] D
Comments
  1. This is a refined version of Script1.
  2. Be careful! The address in Step [4] is 2,6 rather than 1,5, which is used in Step [2] of Script1.