Remove all, or keep one
The right choice depends on what the text is for.
- Remove all suits anything where each line is an item: lists, logs, CSV rows, URLs, keywords, code listings. The result is solid with no gaps, so it can go straight into a spreadsheet, a script or a form.
- Keep one in a row suits prose. A gap between paragraphs is meaningful, but three or four in a row is a mistake. This setting turns every run of empty lines into a single blank line and leaves single ones alone.
If you also want the lines in order or the repeats gone, follow up with sort lines or the duplicate line remover.
Why empty lines appear in the first place
- Double spacing from exports. Some programs write a blank line after every line, or a file has been converted twice between line ending styles so each break turned into two.
- Copying from web pages, PDFs and emails, where paragraph spacing arrives as blank lines.
- Lines that only look empty. A line with a few spaces, a tab or a non breaking space shows nothing but is not zero length, so a simple search for empty lines misses it. This tool counts such lines as empty.
- Invisible characters. A line holding only a zero width space is not treated as empty, since it counts as text. Switch on Remove invisible characters, which runs before the empty line step, to include it.
Doing it in an editor or on the command line
In an editor that supports regular expressions, such as VS Code, turn the regex option on, search for ^\s*\n and replace with nothing. On the command line, sed '/^[[:space:]]*$/d' file.txt deletes lines that are empty or hold only whitespace. A plain sed '/^$/d' deletes only lines of zero length, so lines with spaces survive.
This page does the same thing without a command or a pattern, and shows a count of how many lines went.