Raw Input Desired Output
 
(p1,line-1)
(p1,line-2)
(p1,line-3)
 
 
(p2,line-1)
(p2,line-2)
 
(p3,line-1)
(p3,line-2)
(p3,line-3)
(p3,line-4)
 
 
(p1,line-1)
(p2,line-1)
(p3,line-1)
Script and Comments
Script1
[ 1] /^$/{
[ 2] h
[ 3] d
[ 4] }
[ 5] x
[ 6] /^$/!d
[ 7] g
Comments
  1. 'Hold Space' is abbreviated to 'HS'.
  2. A different approach is used to develop this script in which
    • HS is used as a flag,
    • which is set if HS is not empty, clear otherwise.
    • for each non-blank line, if the flag is clear, then print it; otherwise, discard it.
  3. Each time a blank line is read, Step [2] will overwrite HS with this blank line (i.e., clear the flag) to ensure the first non-blank line following it will be printed.
  4. Each time a non-blank line is read, Steps [5] and [6] are used to check the flag:
    • if the flag is set, then discard that line;
    • otherwise, Step [7] will get that line from HS.
    • after Step [6], the flag is set since HS is not empty.\
Script2
[ 1] /^$/h
[ 2] x
[ 3] /^$/!d
[ 4] g
[ 5] /^$/d
Comments
  1. This script is equivalent to the above one.