Description
Paragraphs are separated by one or more blank lines. The following script will join all lines of a paragraph to one, retaining blank lines.
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) (p1,line-2) (p1,line-3)
 
 
 
(p2,line-1) (p2,line-2)
 
(p3,line-1) (p3,line-2) (p3,line-3) (p3,line-4)
 
Script and Comments
Script1
[ 1] /^$/b
[ 2] :loop
[ 3] $!N
[ 4] s/\n\(.\)/ \1/
[ 5] t loop
Comments
  1. If the current line is a blank one, Step [1] will make sed
    • branch to the end of the script,
    • print it,
    • then start a new cycle.
  2. Otherwise, the loop composed of Steps [2] thru [5] will repeat joining the next line to Pattern Space until an blank line or the end of the datafile is reached.
  3. Command 's' of Step [4] will fail if
    • the last line joined by Step [3] is a blank one or
    • the end of the datafile is reached.
    in this case, the conditional branch of Step [5] will not be taken.