โ† Back to Blog

How to Remove Blank Lines from Text

2026-04-13 ยท 5 min read

Sources of Blank Line Problems

Blank lines appear in text for many reasons: copying from PDF or Word documents preserving format line breaks; repeated pasting of text adding duplicate separator lines; line ending differences between old software or different systems; program-generated text with extra blank line separators; database exports automatically inserting blank lines between records.

Two Processing Needs

There are two different goals for blank line processing: delete all blank lines (suitable for compact no-spacing formats needed in some data processing pipelines); or compress consecutive multiple blank lines to a single blank line (preserving single-line paragraph separators while only deleting redundant duplicate blank lines โ€” the more common need). Online tools typically provide both options.

What Is a "True" Blank Line

A line that looks blank is not necessarily truly empty. A line may contain invisible characters: one or more spaces, tab characters, or non-breaking spaces. These "pseudo-blank lines" are invisible to the eye but cause simple "remove blank lines" operations to miss them. The correct approach: if a line's content after trimming (removing leading and trailing whitespace) is an empty string, treat it as a blank line and delete it.

Command-Line Tools

# ๅˆ ้™คๆ‰€ๆœ‰็ฉบ่กŒ๏ผˆๅŒ…ๆ‹ฌๅชๅซ็ฉบๆ ผ็š„่กŒ๏ผ‰
grep -v '^\s*$' input.txt > output.txt

# ไฝฟ็”จ sed ๅˆ ้™ค็ฉบ่กŒ
sed '/^\s*$/d' input.txt > output.txt

# ๅฐ†่ฟž็ปญๅคšไธช็ฉบ่กŒๅŽ‹็ผฉไธบไธ€ไธช็ฉบ่กŒ
cat -s input.txt  # macOS/Linux๏ผˆ-s: ่ฟž็ปญ็ฉบ่กŒๅŽ‹็ผฉไธบไธ€่กŒ๏ผ‰

# ๆˆ–ไฝฟ็”จ sed
sed '/^$/N;/^\n$/d' input.txt > output.txt

Python Code Implementation

# ๅˆ ้™คๆ‰€ๆœ‰็ฉบ่กŒ
def remove_all_blank_lines(text):
    lines = text.splitlines()
    return '\n'.join(line for line in lines if line.strip())

# ๅŽ‹็ผฉ่ฟž็ปญ็ฉบ่กŒไธบๅ•ไธช็ฉบ่กŒ
def compress_blank_lines(text):
    import re
    return re.sub(r'\n{3,}', '\n\n', text)  # 3ไธชไปฅไธŠๆข่กŒ็ฌฆๅ˜ไธบ2ไธช

# ็ปผๅˆๅค„็†๏ผšๅ…ˆๅŽ‹็ผฉ๏ผŒๅ†ๅŽป้™ค้ฆ–ๅฐพๅคšไฝ™็ฉบ่กŒ
def clean_blank_lines(text):
    import re
    text = re.sub(r'\n{3,}', '\n\n', text)  # ๅŽ‹็ผฉๅคšไฝ™็ฉบ่กŒ
    return text.strip()  # ๅŽป้™ค้ฆ–ๅฐพ็ฉบ็™ฝ

Processing in Word and Document Editors

In Microsoft Word, use the Find and Replace function to batch delete blank lines: open Find and Replace (Ctrl+H), enter ^p^p (two paragraph marks, representing a blank line) in the Find field, enter ^p (one paragraph mark) in the Replace field, and click "Replace All" multiple times until no more replacements occur. This compresses consecutive blank lines to a single blank line.

Blank Line Management in Code Files

In code files, blank lines are meaningful formatting elements used to separate functions, classes, and logical blocks. Python PEP 8 specifies: two blank lines between top-level function and class definitions; one blank line between methods within a class; logical groupings within functions may be separated by one blank line. Code auto-formatters (like black, prettier) automatically normalize blank line counts to maintain consistent code style.

Try the free tool now

Use Free Tool โ†’