Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Deleting Blank Lines Using Grep

June 20, 2023 — Gregg Szumowski

If you have a file (or a stream) that has blank lines you want to remove, you can use this command:

$ grep -v "^ *$" file-with-blanks.txt > output-file.txt

where file-with-blanks.txt is the input file and has blank lines and output-file.txt will be the output file without the blank lines.

You have to keep the space between the ^ and the *$ otherwise blank lines which include spaces will not be removed.

Tags: cli, grep, motd