Linux: remove empty lines from a file with sed

Linux - Remove empty lines from a file

You're working under Linux and want to remove empty lines from a file, but don't know how? This article will answer that question, using two very common commands: grep and sed. We'll look at how to display the contents of a file excluding empty or commented lines, then at how to remove empty and commented lines from a file. When deleting lines, this is done with Caution: this action is irreversible, unless you have a backup or copy of the file.

This method relies on the use of grep and sed commands so it will work on various Linux distributions, including the most common ones: Ubuntu, Debian, CentOS, Rocky Linuxetc.

1. Display file contents without empty lines

Let's start by using a file as an example. For my purposes, this will be the " /tmp/file.txt" with the following content:

Linux - Example of a file with empty lines and comments

In the image above, the empty and commented lines are clearly visible. Now, with the "cat" command, we'll display the contents of the file and filter the output with "grep".

  • Afficher le contenu du fichier sans les lignes vides
cat /tmp/file.txt | grep .
  • Afficher le contenu du fichier sans les lignes commentées (mais avec les lignes vides)

This filter eliminates all lines beginning with the character "#", corresponding to comments.

cat /tmp/file.txt | grep -v ^\#
  • Afficher le contenu du fichier sans les lignes vides et sans les lignes commentées
cat /tmp/fichier.txt | grep . | grep -v ^\#

These three controls are formidable and work very well:

Linux - Using grep to avoid displaying empty lines

Let's move on to the second part of this tutorial, where we'll remove these lines from the file.

2. Removing empty lines from a file with sed

The sed command is a powerful tool for modifying the contents of Linux files, based on regular expressions. With this command, we can delete all the empty lines in our file without altering the other lines.

  • Supprimer les lignes vides du fichier
sed -i '/^$/d' /tmp/file.txt

Here is the result on the file we have been mistreating since the beginning:

Linux - Using sed to remove empty lines from a .jpg file
  • Supprimer les lignes commentées du fichier
sed -i '/^#/d' /tmp/file.txt
  • Supprimer les lignes vides et commentées du fichier

As with "grep", we can combine the two rules to remove both empty and commented lines. Finally, you can choose to use either rule, or both at the same time.

sed -i -e '/^#/d' -e '/^$/d' /tmp/file.txt

Thanks to this article from the Computer Tutorials box, you'll be able to filter the contents of your Linux files. Filtering the display is useful for reducing the output in the console so that it looks smaller on the screen. Deletion is useful when you need to make a massive modification to a file, for example a dataset such as a CSV file.

Resources :

You may also like...

Leave a Reply

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