How To Remove Empty Lines in Bash on Linux Like a Pro

Any Linux users know the potential annoyance that blank lines might pose in a processable file. In addition to impeding the processing of such files, these empty/blank lines make it challenging for running software to read and write the file. This post will look at a few quick methods to remove empty/blank lines in Bash on Linux.

To simplify the subject, we will talk about 6 possible approaches and look at some pertinent cases with the required justifications. We will use different commands and examples to delete the empty line from the file and display the file.

6 Methods to Remove Empty Lines in Bash in Linux

The 6 techniques listed below will be used to remove blank lines in Bash in Linux. Before we begin, let’s see what their functions are:

  • sed command:Stream editor for filtering and manipulating text.
  • grep command: Display lines that match patterns.
  • awk command: The awk utility executes programs written in the awk programming language, tailored for manipulating textual data.
  • cat command: Joins files together and prints them on the standard output.
  • tr command: Translate or remove characters.
  • perl command: A programming language that was created specifically for text editing.

Method 1: Run the sed Command to Remove Empty Lines in Bash

In our first solution, we will use the sed command to remove the empty lines in Bash from the file. The sed command is used to do simple text changes.

  • The stream editor sed can remove blank lines from files, as seen below.
    sed '/^[[:space:]]*$/d' 1_Test.txt
  • When you run the Bash as mentioned above script, you should see an output similar to the one below:
    This is the first line.
    This is the second line.
    This is the third line.
    This is the fourth line.

Wherein:

  • '/[[:space:]]*$/d': Portion of the code is used to find and delete the empty line from the file.
  • //: The search string is stored there.
  • ^: Start of the string.
  • $: End of the string.
  • d: Remove the matched string.
  • txt: Source file name.

Method 2: Remove Blank Lines in Bash by Using the grep Command

Grep stands for Global Regular Expression Print. We can easily remove the Bash lines with the grep command, another built-in tool in Bash. A provided file's text and strings are searched, and the program outputs every line that meets a pattern.

  • You can use this technique to delete the empty line from the file by following the example below.
    grep -v '^[[:space:]]*$' 1_Test.txt
  • The output from running the Bash mentioned above script will look somewhat like this:
    This is the first line.
    This is the second line.
    This is the third line.
    This is the fourth line.

Details are as follows:

  • '[[:space:]]*$': A portion of the code is used to find and delete the empty line from the file.
  • .: Replaces any character
  • ^: Start of the string
  • $: String's end.
  • E: To match patterns in extended regular expressions.
  • e: For pattern matching in regular expressions.
  • v: To pick out any lines that don't match the file.
  • txt: Name of the source file

Method 3: Use the awk command to Remove Empty Lines in Bash

In this method, we'll make advantage of awk, a built-in keyword in Bash Script. The general-purpose scripting language awk was created for sophisticated text data processing. Text manipulation, reporting, and analysis are its main uses.

  • The example below demonstrates removing the empty line from the file using this keyword.
    awk '!/^[[:space:]]*$/' 1_Test.txt
  • And here’s the output:
    This is the first line.
    This is the second line.
    This is the third line.
    This is the fourth line.

Wherein:

  • /[[:space:]]*$/: The blank lines in a file are recognized and removed using the syntax.
  • //: The search string is stored there.
  • ^: Start of the string
  • $: String's end.
  • .: Replaces any character
  • !: Remove the string that matches.
  • txt: Name of the source file

Method 4: Run the cat Command  to Remove Blank Lines in Bash

Cat is an abbreviation for concatenate. It is commonly used in Linux to read data from a file. On Unix-like operating systems, the cat command is one of the most commonly used commands. It provides three text file-related functions: display file content, combine multiple files into a single output, and create a new file.

  • We can easily remove blank lines from a file by combining the commands 'cat' and 'tr' as shown below:
    Cat 1_Test.txt | tr -s '\n' > new_file.txt
  • Output:
    This is the first line.
    This is the second line.
    This is the third line.
    This is the fourth line.

The following are the specifics:

  • |: The pipe symbol. It uses the output of the first command as an input to another command.
  • s: Replace every sequence of a repeated character in the last specified SET.
  • \n: To insert a new line.
  • txt: Name of the source file.

Method 5: Remove Empty Lines in Bash by using the tr Command

In Linux and Unix systems, tr is a command-line utility that translates, deletes, and squeezes characters from standard input and writes the results to standard output. The tr command can remove repeated characters, convert uppercase to lowercase, and perform basic character replacing and removing. It is typically used in conjunction with other commands via piping.

  • Squeeze the repeating newline letters to remove the blank lines:
    tr -s '\n' < file.txt > new_file.txt

In the command above, we used:

  • <: This redirection symbol is used to pass the contents of file.txt to the tr command.
  • >: Redirection symbol that writes the output of the command to new_file.txt.

Method 6: Use the perl Command to Remove Blank Lines in Bash

Perl is a shortened version of Practical Extraction and Reporting Language. Perl is a programming language that was created specifically for text editing. It is now widely used for various tasks such as Linux system administration, network programming, web development, etc.

  • The following example shows how to use this keyword to remove an empty line from a file.
    perl -ne 'print if /\S/' 1_Test.txt > new_file.txt
  • And here's the result:
    This is the first line.
    This is the second line.
    This is the third line.
    This is the fourth line.

Wherein:

  • .: Can be used to replace any character.
  • ^: Beginning of the string.
  • $: End of the string.
  • E: For pattern matching with extended regular expressions.
  • e: Pattern matching for regular expressions.
  • v: Extracting non-matching lines from a file.
  • txt: Name of the source file.

How To Recover Accidentally Deleted Files in Linux

Now that we've discussed six different methods to remove empty lines in Bash on Linux, it's important to note that accidents can happen, and sometimes important files can be accidentally deleted. Fortunately, there are ways to recover deleted files in Linux.

One highly recommended solution is to use Wondershare Recoverit Linux File Recovery. This powerful data recovery software is designed specifically for Linux systems and can quickly and easily recover deleted files, even if they have been permanently deleted from the trash or recycle bin.

Wondershare Recoverit - Your Safe and Reliable Linux Recovery Software

5,481,435 people have downloaded it.

Recovers lost or deleted documents, photos, videos, music, emails, and other 1000+ file types effectively, safely, and completely.

Compatible with all mainstream Linux distros, including Ubuntu, Linux Mint, Debian, Fedora, Solus, Opensuse, Manjaro, etc.

Assists in 500+ data loss scenarios, such as deletion, disk formatting, OS crash, power outage, virus attack, lost partition, and many more.

The simple point-and-click interface allows you to recover data from Linux hard drives in just a few clicks.

Works through a remote connection. You can recover lost data even when your Linux device is crashed.

Linux data recovery is really easy and hassle-free with Wondershare Recoverit. Your data can be recovered in 3 simple steps.

Step 1Choose Linux Recovery

Download and install Wondershare Recoverit on your computer. Open the software, then select NAS and Linux. To continue, click the Linux Recovery option.

choose the linux recovery option
Step 2Remotely Connect the Linux Computer

As seen below, a new window will open on your screen. Enter the required data to establish a remote connection. Once finished, Click the Connect button.

remotely connect the linux computer

Recoverit will scan automatically to look for your missing files in a Linux computer once the connection has been established successfully.

scan linux deleted/lost files
Step 3Preview and Recover

The nicest part about Recoverit is that you can stop scanning anytime when you find the file you want to restore. You can preview the files to ensure they are the ones you want to recover. To save the desired files, select them and click the Recover button now.

preview deleted/lost files

The software will prompt you to select the location where you want to keep the recovered files. To obtain the restored data, click Recover.

recover and save deleted/lost files
Free Download

For Windows Vista/7/8/10/11

Free Download

For macOS X 10.10 or later

Summing Up

We discussed the six methods to remove empty lines in Bash on Linux, and in case of accidental data loss, Wondershare Recoverit Linux Recovery is a recommended software to get your data loss back safely and hassle-free!

You May Also Like

Best Alternatives to Recuva for Linux

This article introduces Recuva and its limitations while offering its best alternatives.

How To Undelete Files Using debugfs in Linux

A guide to recover deleted files using debugfs - a Linux interactive file system debugger.

How To Recover Data From LVM Partitions on Linux

Learn step-by-step procedures to restore data from LVM partitions on Linux systems.

How To Undo rm in Linux

What can you do to protect Linux data from the accidental running of the RM command?