deblank: Turn multiple blank lines into single blank lines


This shell script will read an ASCII text file and remove multiple blank lines, leaving a single blank line from each sequence of blank lines. It is useful for reducing the length of files with many blank lines without removing the intent that there should be blank space after sections of text.

The programming problem is to remove the second and following blank lines, but leave the first blank line. The sed command used does this by finding a blank line, then going to the adjacent blank line and removing it, etc. The program uses a line of Perl to check the script is not running on a binary file before issuing the sed command. See the code linked below.

This utility is part of a collection of more text-processing tools.

Usage

The usage is simple: deblank [filename...] An example run is shown below. First the deblank command is run with no filename and a usage message is given in that case. Next deblank is run on a text file with multiple blank lines mixed with text and we can see the result.

 -------------------------------------------------------------
  deblank: Turns multiple blank lines into single blank lines
 -------------------------------------------------------------
     
 Needed a filename (and only one filename) to convert!
 If more than one filename is given, the second and
 succeeding files will be ignored. (You may, however, redirect
 the output to a file).
         
    Usage:
          deblank [filename]

Assume we have a small text file with some blank lines in it. If we want to remove duplicate or multiple blank lines, deblank will do the job.

This is the original text file, test1.

> more test1

a file with several lines.
a file with several lines.
                          <- blank line
                          <- blank line
                          <- blank line
                          <- blank line
a file with several lines.
                          <- blank line
                          <- blank line
a file with several lines.
Now deblank is run on test1 and the multiple blank lines are processed.
>deblank test1

a file with several lines.
a file with several lines.

a file with several lines.

a file with several lines.

Above, deblank has removed the multiple blank lines and retained one blank line from sets of multiple blank lines.
Back to Kent's Perl Page

Last Modified: Tue Sep 9 10:24:43 EDT 2025