Quickzi: How To Remove Blank Lines from a File

If you want to remove all blank lines from a file, a quick way of doing it from the Linux command line is to use sed.

So, lets say that your file looks like this:

# vi example_file

This is an example text file.
We will be using sed to remove blank lines from this example file.

sed is a stream editor for filtering and transforming text

There are a lot of powerful things you can do with sed.

:wq

Now, to remove the blank lines in this file, we’ll use sed.

# sed -i '/^$/d' example_file

# cat example_file

This is an example text file.
We will be using sed to remove blank lines from this example file.
sed is a stream editor for filtering and transforming text
There are a lot of powerful things you can do with sed.

If you want to preserve the original file, then you can pipe the changes to a new file:

sed '/^$/d' example_file > example_file_new

Cheers!