Few things are as quietly annoying as a file riddled with blank lines. They creep in from bad exports, sloppy editing, or scripts that append without checking. StackAbuse recently walked through how sed can wipe them out in seconds.
Here's the version that actually sticks.
At a glance
- Sed is the stream editor bundled with Linux.
- /^$/ matches lines containing no characters at all.
- Pairing that pattern with the d command deletes them.
- Pass -i to edit the file directly.
- For lines holding only tabs or spaces, swap in \s*.
What sed is doing here
Sed reads a file line by line, applies the patterns you give it, and prints the result. When the pattern matches, the matched line is acted on. The d command tells sed to drop that line entirely. The pattern /^$/ anchors to the beginning and end of a line with nothing between them, so it only catches lines that are truly blank.
To wipe those from a file directly, you'd run something like sed -i '/^$/d' report.txt. The -i flag means sed rewrites the file in place rather than just dumping the output to your terminal.
Our analysis
This is the kind of one-liner you'll reach for constantly. Log files, CSV dumps, JSONL exports scraped from a website, all of them arrive with random gaps. Cleaning them up before you pipe them into another tool saves a lot of headaches downstream.
The main gotcha is what "empty" actually means. A line with a single space looks blank in your editor, but the basic /^$/ pattern won't touch it. That's not a bug. It's regex being literal. If you want to nuke whitespace-only lines too, broaden the pattern to /^\s*$/. The \s* lets any amount of whitespace sit between the anchors, and sed still deletes the line.
Trade-offs: in-place editing is fast but irreversible. Always either make a backup first or run the command without -i and pipe the output somewhere safe. Drop -i and sed prints the cleaned file to stdout, which is perfect for testing.
Who benefits: anyone running scripts on Linux, doing data wrangling, or maintaining configs. Who loses: nobody. It's a small command with a very specific job.
Beyond this trick, sed is worth getting comfortable with for general text cleanup. The pattern-plus-command structure composes well, and you'll find yourself combining pieces for tasks like numbering lines, substituting text, or extracting ranges.
Worked example: tidying a messy export
Picture this: you've pulled a customer list from a system that pads every entry with gaps. The raw output has rows like:
ID: john_doe; [email protected]; New York, USA
ID: jane_smith; [email protected]; London, UK
ID: bob_marshall; [email protected]; Sydney, Australia
ID: alice_johnson; [email protected]; Toronto, Canada
Before you hand the file off to your import script, you want it tidy. Run sed -i '/^$/d' users.txt and the gaps disappear instantly, just like the StackAbuse walkthrough showed. The file is now a clean sequence of four rows, nothing more.
If the export also leaves trailing whitespace on every line, upgrade the pattern to sed -i '/^\s*$/d' users.txt. That hits both fully blank rows and rows that only hold spaces or tabs.
Want to be cautious? Skip the -i flag and redirect instead: sed '/^$/d' users.txt > cleaned.txt. Now you've got a separate file to inspect before deciding to replace the original.
What to watch next
Keep an eye on awk for similar jobs. When your cleanup logic grows beyond a single pattern, awk gives you more flexibility with conditional logic and field-aware operations. For pure blank-line removal, though, sed still wins on brevity.
Bottom line: learn sed -i '/^$/d', add \s* when you need it, and keep a backup habit. That's the whole game.
Key takeaways
- /^\s*$/d catches lines with zero characters; add \s* for whitespace-only lines.
- The -i flag edits in place; remove it to preview safely.
- Always test on a copy or preview the output first.
Frequently asked questions
Will this also strip lines full of spaces or tabs?
Not with the basic pattern. Use /^\s*$/d instead to catch whitespace-only lines.
How do I preview before overwriting?
Run sed without the -i flag and redirect the output to a new file so you can inspect it first.
Does this work on macOS?
Yes, but macOS sed wants an empty string after -i, so use sed -i '' '/^$/d' file.
What about really large files?
Sed is fast and streams the file, so size is rarely an issue. Still, keep a backup just in case.
Source: stackabuse.com