Skip to content
ToolkitHub

Remove Empty Lines

Remove empty lines from text

Guide

Removing blank lines without losing structure

By Logappradeep · ServiceNow developer

Text copied from PDFs, web pages, emails, chat apps, and terminal output often arrives with extra blank lines between every line or paragraph. Those gaps get in the way when you are building a list, importing data, or comparing files. This tool deletes every line that is empty or contains only whitespace and leaves the rest exactly as it was.

What counts as empty

A line is removed when it has no characters, or when every character is whitespace, such as spaces or tabs. That second case matters because lines that look blank often contain stray spaces from copy and paste. By default, lines with real content are not trimmed or changed in any way, so indentation in code or nested lists is preserved. Tick Also trim whitespace from remaining lines if you want leading and trailing spaces removed too.

first item

   
second item
    indented detail

third item

becomes:

first item
second item
    indented detail
third item

When blank lines carry meaning

Before you strip every blank line, check whether the format relies on them:

  • Markdown and plain-text prose use a blank line to separate paragraphs. Removing them merges all paragraphs into one block.
  • HTTP messages and email use an empty line to separate headers from the body.
  • Code style often relies on blank lines between functions for readability. Python's PEP 8, for instance, recommends two blank lines around top-level definitions.
  • CSV files may contain blank lines inside quoted multi-line values, where removing them changes the data.

For prose, you may want to collapse multiple blank lines into one rather than remove them all. That is easy to do in most code editors with a find-and-replace using regular expressions.

Command-line equivalents

If you need to do this in a script, grep -v '^[[:space:]]*

file.txt prints only non-blank lines, and sed '/^[[:space:]]*$/d' file.txt does the same. The [[:space:]] class matches spaces and tabs, mirroring this tool's handling of whitespace-only lines.

Common uses

  • Cleaning up lists pasted from documents or spreadsheets before importing them.
  • Tidying log excerpts and command output before sharing them in a ticket.
  • Preparing data for tools that treat a blank line as the end of input.
  • Compacting copied content from websites that use blank lines for spacing.

Combine this with Remove Duplicates and Sort Lines for a complete list cleanup. All processing happens locally in your browser.

Frequently asked questions

Will it remove lines that contain only spaces?

Yes. Lines made up entirely of spaces or tabs are treated as empty and removed.

Does it trim spaces from the lines it keeps?

Not unless you ask. Lines with content are left exactly as they were unless you tick Also trim whitespace from remaining lines.

How do I keep single blank lines between paragraphs?

This tool removes all blank lines. To collapse several blank lines into one, use your editor's regex find-and-replace instead.

Can I also remove duplicate lines?

Yes, run the output through Remove Duplicates afterward.