How to Remove Blank Lines from Text
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 โ