Raw Input Desired Output
Line 1
Line 2
Line 3



Line 1
Line 2
Line 3
Script and Comments
Script1
[ 1] /^$/{
[ 2] :loop
[ 3] $d
[ 4] N
[ 5] /\n$/b loop
[ 6] }
Comments
  1. Flowchart:
  2. Each time we find a blank line, we append next line to see whether it is a blank one or not:
    • If yes, these two lines may be part of the trailing lines at end of file. We repeat the append-check steps (Step[5]-[2]-[3]-[4]) until a non-blank one is found or till the end of file.
    • If no, the blank line is not part of the trailing blank lines at end of file, we leave it in pattern space. At the end of this iteration, sed will print it and begins a new cycle.