Deleting empty lines from text files (sed)

Last modified on July 24, 2026 • 1 min read • 53 words
The following sed command deletes empty lines (and those with white spaces only) from text files.

The following sed command deletes empty lines (and those with white spaces only) from text files:

sed -e '/^ *$/d' infile.txt > outfile.txt

You have to be careful though: if you copied the textfile from a Macintosh this script maybe won’t work. You first have to convert the Macintosh textfile into a UNIX textfile!