Remove Empty Lines

Remove empty or blank lines from text. Optionally treat whitespace-only lines as empty.

Back to all tools on ToolForge

More in Text Tools

Input





Result

About Remove Empty Lines

This tool removes empty lines from text by filtering out lines that contain no content or only whitespace. It splits input by line breaks, applies a filter function to each line, and joins the remaining lines back together. The optional trim mode treats whitespace-only lines as empty.

It is useful for cleaning up log files with excessive spacing, compacting code output, preparing data for CSV import, removing paragraph breaks from copied text, condensing email threads, and reducing vertical whitespace in documentation.

Empty Line Detection

Empty lines are identified using string operations:

Empty Line Types:

1. Truly Empty Line
   - Contains zero characters
   - JavaScript: line === ""
   - Example: "line1\n\nline2" (empty line between)

2. Whitespace-Only Line
   - Contains only spaces, tabs, or other whitespace
   - JavaScript: line.trim() === ""
   - Example: "line1\n   \nline2" (3 spaces on middle line)

3. Mixed Whitespace
   - Contains combination of spaces and tabs
   - JavaScript: line.trim() === ""
   - Example: "line1\n  \t  \nline2"

Detection Logic:
  // Strict mode (only truly empty)
  lines.filter(l => l !== "")

  // Trim mode (empty + whitespace-only)
  lines.filter(l => l.trim() !== "")

Whitespace Characters Reference

Character Name Unicode JavaScript
Space U+0020 " "
Tab U+0009 "\t"
Line Feed U+000A "\n"
Carriage Return U+000D "\r"
Non-breaking Space U+00A0 "\u00A0"
En Space U+2002 "\u2002"
Em Space U+2003 "\u2003"

Filtering Methods Comparison

Method 1: Remove Only Empty Lines
  Input:
    "Line 1

    Line 2
       "

  Output:
    "Line 1
    Line 2
       "

  Keeps whitespace-only lines

Method 2: Remove Empty + Whitespace-Only
  Input:
    "Line 1

    Line 2
       "

  Output:
    "Line 1
    Line 2"

  Removes all blank lines

Method 3: Collapse Multiple Empty Lines
  Input:
    "Line 1




    Line 2"

  Output:
    "Line 1

    Line 2"

  Keeps single blank lines

Method 4: Remove and Trim Each Line
  Input:
    "  Line 1

  Line 2  "

  Output:
    "Line 1
    Line 2"

  Trims leading/trailing whitespace

Common Use Cases

Use Case Description Trim Mode
Log File Cleanup Remove excessive spacing from log output Enabled
Code Compaction Reduce vertical space in code listings Optional
CSV Data Cleaning Remove blank rows before import Enabled
Email Thread Cleanup Condense forwarded email chains Enabled
Markdown Formatting Adjust paragraph spacing in documents Disabled
Data Export Cleaning Remove blank lines from exported data Enabled
Terminal Output Compact command output for sharing Enabled

Code Examples by Language

JavaScript:
  // Remove empty lines only
  text.split(/\r?\n/).filter(l => l !== "").join("\n")

  // Remove empty + whitespace-only
  text.split(/\r?\n/).filter(l => l.trim() !== "").join("\n")

  // Collapse multiple empty lines
  text.replace(/\n{3,}/g, "\n\n")

  // Using regex to filter
  text.split(/\r?\n/).filter(l => /\S/.test(l)).join("\n")

Python:
  # Remove empty lines only
  '\n'.join([l for l in text.split('\n') if l != ""])

  # Remove empty + whitespace-only
  '\n'.join([l for l in text.splitlines() if l.strip()])

  # Collapse multiple empty lines
  re.sub(r'\n{3,}', '\n\n', text)

Bash/sed:
  # Remove empty lines
  sed '/^$/d' file.txt

  # Remove empty + whitespace-only
  sed '/^[[:space:]]*$/d' file.txt

  # Using grep
  grep -v '^[[:space:]]*$' file.txt

PowerShell:
  # Remove empty lines
  Get-Content file.txt | Where-Object { $_ -ne "" }

  # Remove whitespace-only
  Get-Content file.txt | Where-Object { $_.Trim() -ne "" }

PHP:
  // Remove empty lines
  implode("\n", array_filter(file('file.txt'), fn($l) => trim($l) !== ""));

  // Using preg_filter
  preg_filter('/^\s*$/m', '', $text)

Regex Patterns for Empty Lines

Pattern        | Matches                    | Description
---------------|----------------------------|------------------
^$             | Empty lines only           | Start + end anchor
^\s*$          | Empty + whitespace-only    | Zero or more whitespace
^[\t ]*$       | Space/tab only lines       | Explicit space/tab
^\h*$          | Horizontal whitespace      | POSIX horizontal class
^(?=\s*$)      | Lookahead for blank lines  | PCRE/JavaScript
[\r\n]{3,}     | Multiple consecutive       | 3+ line breaks

JavaScript Examples:
  // Test if line is empty or whitespace-only
  /^\s*$/.test(line)

  // Remove all empty/whitespace lines
  text.split(/\r?\n/).filter(l => !/^\s*$/.test(l)).join("\n")

  // Collapse 3+ newlines to 2
  text.replace(/[\r\n]{3,}/g, "\n\n")

  // Keep lines with at least one non-whitespace
  text.split(/\r?\n/).filter(l => /\S/.test(l)).join("\n")

Empty Line Removal Examples

Example 1: Simple Empty Lines
  Input:
    "Line 1

    Line 2

    Line 3"

  Output:
    "Line 1
    Line 2
    Line 3"

Example 2: Whitespace-Only Lines
  Input:
    "Line 1

    Line 2

    Line 3"

  With trim: "Line 1\nLine 2\nLine 3"
  Without trim: Keeps whitespace lines

Example 3: Multiple Consecutive Empty Lines
  Input:
    "Header




    Body




    Footer"

  Collapse to single blank:
    "Header

    Body

    Footer"

Example 4: Log File Cleanup
  Input:
    "2024-01-15 INFO: Started

    2024-01-15 ERROR: Failed

    2024-01-15 INFO: Retrying"

  Output:
    "2024-01-15 INFO: Started
    2024-01-15 ERROR: Failed
    2024-01-15 INFO: Retrying"

Example 5: Code with Excessive Spacing
  Input:
    "function test() {

      console.log("Hi");

    }"

  Output:
    "function test() {
      console.log("Hi");
    }"

Example 6: CSV Data Cleaning
  Input:
    "name,age

    John,30

    Jane,25"

  Output:
    "name,age
    John,30
    Jane,25"

Example 7: Email Thread
  Input:
    "From: John

    > From: Jane
    >
    > Original message

    Reply here"

  Output:
    "From: John
    > From: Jane
    >
    > Original message
    Reply here"

Example 8: Mixed Content
  Input:
    "Paragraph 1.


    Paragraph 2.

    Paragraph 3."

  Output (trim mode):
    "Paragraph 1.
    Paragraph 2.
    Paragraph 3."

Best Practices

Limitations

Frequently Asked Questions

What constitutes an empty line?
An empty line contains no characters (zero-length string). A blank line may contain only whitespace characters (spaces, tabs) but no visible content. This tool can remove both truly empty lines and whitespace-only lines depending on the selected option.
What is the difference between empty and whitespace-only lines?
Empty lines contain nothing (zero characters between line breaks). Whitespace-only lines contain spaces, tabs, or other invisible characters but no visible text. For example, '\n\n' has empty lines; '\n \n' has whitespace-only lines with 3 spaces.
How does the whitespace trimming option work?
When enabled, the tool uses String.trim() to remove leading and trailing whitespace from each line before checking if it's empty. Lines containing only spaces/tabs are treated as empty and removed. This is useful for cleaning up indented code or formatted text.
What are common use cases for removing empty lines?
Common uses include: cleaning up log files with excessive spacing, compacting code output, preparing data for CSV import, removing paragraph breaks from copied text, condensing email threads, and reducing vertical whitespace in documentation.
How can I remove multiple consecutive empty lines but keep single empty lines?
Use a regex pattern like /\n{3,}/g to find 3+ consecutive newlines (which represent 2+ empty lines) and replace with \n\n. This collapses multiple blank lines while preserving paragraph breaks. Many text editors support this in find/replace.
Does this tool preserve the order of non-empty lines?
Yes, the tool uses a filter operation that preserves the original order of all non-empty lines. Only empty or whitespace-only lines are removed. Line sequence, indentation within lines, and content remain unchanged.