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-3)
(p2,line-2)
(p3,line-4)
Script and Comments
Script1
[ 1] /^$/d
[ 2] $q
[ 3] N
[ 4] /\n$/P
[ 5] D
Comments
  1. Pattern Space is abbreviated to 'PS'.
  2. If the current line is a blank one, Step [1] will discard it and start a new cycle.
  3. Otherwise, if the current line is the last one of datafile, it must be the last one of some paragraph. Step [2] is used to print it and terminate sed.
  4. Otherwise, Step [3] will append the next line to PS; there will be two lines in PS.
  5. If the second line of PS is blank, the first line must be the last one of some paragraph. Step [4] will print it.
  6. Step [5] is used to delete the first line of PS, make sed branch to Step [1].
Script2
[ 1] /^$/d
[ 2] $!N
[ 3] /\n./!P
[ 4] D
Comments
  1. This script is equivalent to the above one.
Script3
[ 1] /^$/d
[ 2] N
[ 3] /\n$/P
[ 4] D
Comments
  1. When perform command 'N' on the last line of datafile, GNU sed will print it then terminate. Using this feature, Steps [2] and [3] of the last script can be written in a neater way.