There are two possible methods to delete empty lines from a text file using sed; the methods are listed below and are discussed in detail in the upcoming part of this guide.

  • Method 1: How to delete all empty lines in a text file using sed
  • Method 2: How to delete specific lines from text file using sed

Method 1: How to delete all empty lines in a text file using sed

Before digging into the depth of this method, let’s understand the syntax to delete empty lines using sed:

Syntax

sed [options]/^$/d’ [file name]

The ‘/^$/d’ is the core part of this command; where “^” symbol shows that the deletion must be executed from the beginning means from first line; “$” represents that it must go till the last line of the text file and “d” shows that deletion is in progress.

This section will guide you to delete all the lines in a text file with the help of stream editor (sed):

We have created a text file “delete.txt; firstly, get the content of this file using “cat” command as given below and we have used “-n” option with it, so that we can get the line numbers too:

It is observed that there are multiple empty lines, and they are affecting the aesthetics of this text file and readers may not pay attention to such content.

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-9.png" data-lazy- height="335" src="data:image/svg xml,” width=”815″>

So, to avoid the above stated situation; you must remove the empty lines to streamline the reading process; the command mentioned below will remove all these lines from the “delete.txt” file.

Now you can notice that the empty lines are washed out and only those lines are printed that contains some text, but the result is only displayed on terminal while the original file remains the same:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-10.png" data-lazy- height="488" src="data:image/svg xml,” width=”809″>

If you want to remove the empty lines and update the original file too then you must use in-place option “-i” and the below-mentioned command will help you to do so:

$ sed -i/^$/d’ delete.txt

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-11.png" data-lazy- height="204" src="data:image/svg xml,” width=”814″>

Method 2: How to delete selected empty lines in a text file using sed

The syntax to delete specific lines in a text file is written below:

Syntax

sed [options](line-number)d’ [filename]

The main part of syntax on which the command relies is “(line-number)d’”; you have to put the exact line number of empty line in “(line-number)” and the letter “d” shows the inserted line number will be deleted:

You can delete some specific lines inside a text file using sed command; we have created a new text file “new.txt” for this section. For instance, the output of below command shows that line number “2” is empty:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-12.png" data-lazy- height="131" src="data:image/svg xml,” width=”688″>

And if you want to run the command to delete this line only then you have to specify the line number like we did in the command mentioned below:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-13.png" data-lazy- height="112" src="data:image/svg xml,” width=”686″>

You can also delete consecutive lines by using this method; for example, the “new.txt” file has 3 empty lines “4,5,6” as can be seen in the image below:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-14.png" data-lazy- height="163" src="data:image/svg xml,” width=”689″>

To remove these three consecutive lines; you must insert “,” between the starting and ending line numbers as can be seen in the command below:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-15.png" data-lazy- height="114" src="data:image/svg xml,” width=”690″>

Lastly, you can also use in place option “-i” to save the changes permanently to the file as without this option the sed command prints the result on the terminal as we have modified the above command to use it with “-i” option:

<img alt="Text Description automatically generated" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/text-description-automatically-generated-16.png" data-lazy- height="129" src="data:image/svg xml,” width=”690″>

Conclusion

Ubuntu supports multiple ways to manipulate the data in a text file; for instance, you can use the default text editor of Ubuntu, the nano editor etc. However, the sed command line utility of Ubuntu is leading all these editors because of its functionalities like accessing the file from the terminal and making changes without opening it. In this article, we have used sed command to remove the empty lines from the text file and described two methods for this operation. “Method 1” is suitable specifically when you have hundreds of lines in a text file and you want to delete all empty lines at once: On the other hand, “Method 2” is suitable for deleting the empty lines in a small document where you can delete lines one by one. However, you have to look for empty lines yourself, if you want to follow “Method 2”: So, if the comparison is drawn between both methods, “Method 1” outperforms “Method 2” about deletion of empty lines.