How to Remove Empty Line in Bash

MD Aminul Islam Feb 02, 2024
  1. File to Remove Empty Line in Bash
  2. Use the sed Keyword to Remove Empty Line in Bash
  3. Use the grep Keyword to Remove Empty Line in Bash
  4. Use the awk Keyword to Remove Empty Line in Bash
How to Remove Empty Line in Bash

When working with files, you may need to remove the empty lines from the file before displaying the file. In Bash Script, there are several ways to remove the empty lines from the file.

In this article, we will see some easy ways to remove the empty lines from the file. We will discuss three different methods and see some relevant examples with necessary explanations to make the topic easier.

We will use three keywords to remove the empty line from the file and display the file. These keywords are sed, grep, and awk; we will see examples for each.

File to Remove Empty Line in Bash

Before we start, suppose we have a text file with the below contents:

This is the first line.
This is the second line.

This is the third line.
This is the fourth line.

Now we are going to use the below three methods one by one to remove that empty line from the file,

Use the sed Keyword to Remove Empty Line in Bash

In our first method, we will use sed to remove the empty line from the file. This is a built-in command in Bash.

To remove empty lines from the file, you can follow the example below:

sed '/^[[:space:]]*$/d' 1_Test.txt

Here the part of code '/^[[:space:]]*$/d' is used to detect and remove the empty line from the file.

After executing the above Bash script, you will get an output like the one below:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

Use the grep Keyword to Remove Empty Line in Bash

We can also eliminate the empty lines by using another built-in command in Bash called grep. You can follow the example below to remove the empty line from the file by using this method.

grep -v '^[[:space:]]*$' 1_Test.txt

Here, the part of code '^[[:space:]]*$' is used to detect and remove the empty line from the file.

After executing the above Bash script, you will get an output like the following:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

Use the awk Keyword to Remove Empty Line in Bash

In this method, we will use another built-keyword in Bash Script, also known as awk. Using this keyword, you can follow the example below to remove the empty line from the file.

awk '!/^[[:space:]]*$/' 1_Test.txt

Here, the part of code '!/^[[:space:]]*$/' is used to detect and remove the empty line from the file.

After executing the above Bash script, you will get the output below:

This is the first line.
This is the second line.
This is the third line.
This is the fourth line.

You can choose any one of the above methods based on your needs.

All the codes used in this article are written in Bash. It will only work in the Linux Shell environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Bash File