delete empty lines from text file in linux

How to Delete Empty Lines from Text File in Linux

Many times you may come across text files with empty lines between and wonder how to remove them from your file. It can be tedious to manually remove empty lines if there are too many of them. Luckily there are many useful Linux commands to help you delete empty lines from text file in Linux. In this article, we will look at how to delete empty lines from text file in Linux. You can use them in almost every Linux distribution as these commands are universally available.


How to Delete Empty Lines from Text File in Linux

We will look at how to remove empty lines form text file using awk, sed and grep command, one by one. Let us say you have the following file at /home/ubuntu/data.txt

Hello. Good Morning.

Hello. Good Morning.

Hello. Good Morning.


1. Using awk

Awk is a powerful text processor with tons of useful commands. For our example, we will use NF variable along with awk command to count the number of columns in each line. If it is not 0, that is, it is not an empty line, we display it.

$ awk '{if(NF>0) {print $0}}' /home/ubuntu/data.txt

Here is the output.

Hello. Good Morning.
Hello. Good Morning.
Hello. Good Morning.

To remove empty lines from the text file, just redirect the output back to your original file. Please use double quotes around the file path after redirection operator.

$ awk '{if(NF>0) {print $0 > "/home/ubuntu/data.txt"}}' /home/ubuntu/data.txt 


2. Using Sed command

Sed command is another powerful text editor that allows you to easily remove empty text lines from your file. We simply use a regular expression to make sed check if each line is empty or not.

sed '/^$/d' /home/ubuntu/data.txt

Here is the output.

Hello. Good Morning.
Hello. Good Morning.
Hello. Good Morning.

Please note, the above sed command will display output to terminal and not edit the file. If you want to make changes to the original file, use -i option as shown below.

$ sed '/^$/d' -i /home/ubuntu/data.txt
$ cat /home/ubuntu/data.txt
Hello. Good Morning.
Hello. Good Morning.
Hello. Good Morning.


3. Using grep command

You can also use grep command to delete empty lines in text file. It also works in a similar to sed, using a regular expression to look for empty lines, and excluding them. We use a regular expression to specify empty lines and use -v option to exclude matched searches.

$ grep -v '^$' /home/ubuntu/data.txt 
Hello. Good Morning.
Hello. Good Morning.
Hello. Good Morning.

Please note, with grep command, you cannot overwrite the output to the same original file. You will need to write to another file and then use cp command to copy it to the original file.

$ grep -v '^$' /home/ubuntu/data.txt > /home/ubuntu/new_data.txt
$ cp new_data.txt data.txt
$ cat new_data.txt
Hello. Good Morning.
Hello. Good Morning.
Hello. Good Morning.

In the article, we have looked at 3 different ways to delete empty lines in text file in Linux. You can use them in any Linux distribution, and also for non-text files.

Also read:

How to Install Supervisor in RHEL/Fedora/CentOS
How to Install OpenSSL in Ubuntu
How to Reset Root Password in Ubuntu
How to Sort CSV File in Python
How to Read Binary File in Python

Leave a Reply

Your email address will not be published. Required fields are marked *