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:]]*