Raw Input Desired Output
some words
some words


some words
some words


some words
some words
some words

some words
some words

some words
Script and Comments
Script1
[ 1] /^$/{
[ 2] N
[ 3] /^\n$/D
[ 4] }
Comments
  1. 'Pattern space' is abbreviated to 'PS'.
  2. If we find a non-blank line, just print it.
    Since a non-blank line does not match '^$', no commands inside /^$/{...} will be performed. Then sed goes to the end of the script. Without command line option '-n', sed will print the contents of PS, and start a new cycle.
  3. Each time we find a blank line, say Linex:
    • To see whether next line (Linex+1) is a blank one or not, we append next line via command 'N' in step [2].
    • Now, we have two lines, Linex and Linex+1, separated by a new line character, in PS.
    • If both lines are blank ones, where PS matches '^\n$', we delete Linex and go to step [2].
      Using command 'D' here is a little bit tricky: it will delete the first line of PS thus make it empty, then goes to the top of script (step [1]). An empty PS matches '^$', so control will go to step [2].
    • If Linex+1 is NOT a blank one, just print Linex and Linex+1.