Raw Input Desired Output
12345678.
123456789012
123456
1234567890123456
1234567890
123456
1234567890123456
Script and Comments
Script1
[ 1] G
[ 2] h
[ 3] s/[^#]/#/g
[ 4] s/\(#*\)\n\1/\n/
[ 5] G
[ 6] /^#/s/.*\n\(.*\)\n.*/\1/
[ 7] s/.*\n//
[ 8] h
[ 9] $!d
Comments
  1. 'Pattern space' and 'Hold space' are abbreviated to 'PS' and 'HS', respectively.
  2. In this script, we use HS to keep the longest line found so far.
  3. Each time after reading a line into PS, we append the longest line kept in HS to PS, separate them by a newline character. This is done via step [1].
  4. After step [1], PS looks like
    .
    Step [2] will keep one copy of this in HS.
  5. Step [3] will translate all characters except new line character to '#'.
  6. Step [4] will remove as many as possible the same number of '#'s at both side of the new line character.
  7. After step [5], if the current line is longer than the longest line found so far, PS will be

    otherwise
    .
  8. After Step [6] and [7], PS will contains the longest line found so far. Step [8] will keep one copy of it in HS.