All of the above solutions will also remove lines that contain nothing but whitespace. If you only want to remove empty lines, and leave lines that contain spaces or tabs etc, change the /\S/ to /^$/ in all of the above examples.
Sometimes the other commands will fail to remove blank lines if the lines contain a blank space. The following will remove all blank lines including lines that contain only blank spaces.
Using sed
Type the following command:
Using grep
Type the following command:
Many ways:
Use
sed
and edit the file in place.Same, but with Perl:
Newer versions of GNU
awk
, in place again:All of the above solutions will also remove lines that contain nothing but whitespace. If you only want to remove empty lines, and leave lines that contain spaces or tabs etc, change the
/\S/
to/^$/
in all of the above examples.You can use Vim in Ex mode:
/./
find non blank linesv
invert selectiond
deletex
save and closeYou can do this in the default Ubuntu Text Editor (gedit) by:
Ctrl
+H
to open Find and Replace\n\n
which signifies two new lines without text in-between them.\n
If the text file was produced in windows, you may want to try
\r\n
.Sometimes the other commands will fail to remove blank lines if the lines contain a blank space. The following will remove all blank lines including lines that contain only blank spaces.