Raw Input Desired Output
 
==Paragraph-1 Line 1===
==Paragraph-1 Line 2===
==Paragraph-1 Line 3===
 
==Paragraph-2 Line 1===
==Paragraph-2 Line 2===
 
 
==Paragraph-3 Line 1===
==Paragraph-3 Line 2===
==Paragraph-3 Line 3===
==Paragraph-3 Line 4===
 
==Paragraph-1 Line 3===
==Paragraph-2 Line 2===
==Paragraph-3 Line 4===
Script and Comments
Script1
[ 1] /^$/d
[ 2] $!N
[ 3] s/^\([^\n]*\)\n*$/\1/
[ 4] t
[ 5] D
Comments
  1. 'Pattern space' is abbreviated to 'PS'.
  2. Paragraphs are separated by one or more blank lines.
  3. A non-blank line is the last line of some paragraph if
    • it is followed by a blank line, or
    • it is the last line of the datafile.
  4. Step [1] will delete any blank line read-in at the start of every cycle.
  5. For every non-blank line, to decide whether it is the last line of some paragraph, we have to check the next line. Command 'N' is used to join the next line.
  6. After Step [2], PS may contain
    (a) ^..line N..\n..line N+1..$
    (b) ^..line N..\n$
    (c) ^..line N..$
  7. Step [3] is used to remove the possible newline char at the end of PS. Lines N in case (b) and (c) are what we want, and they will make the substitution successful.
  8. If you want to separate any two consecutive lines of the output by a blank one, you can use
    /^[^\n]*\n*$/b
    to replace Steps [3] and [4].