Remove Blank Lines From a File in Linux


Introduction

When working with files in Linux, it is common to come across files that contain blank lines. These blank lines can make it difficult to read file, especially when dealing with large files. In this article, we will discuss different methods to remove blank lines from a file in Linux.

Why Remove Blank Lines from a File?

There are several reasons why you may want to remove blank lines from a file. First, it makes file easier to read, especially when dealing with large files. Second, it can help reduce file size, which can be beneficial when transferring or storing files. Finally, removing blank lines can be important when dealing with scripts or programs that expect specific input formats.

Methods to Remove Blank Lines from a File

There are several methods that you can use to remove blank lines from a file in Linux. In this section, we will discuss some of most common methods.

Method 1: Using sed Command

The sed command is a powerful text editor that can be used to perform various text manipulation tasks, including removing blank lines from a file. Here's syntax to remove blank lines from a file using sed command −

sed '/^$/d' inputfile > outputfile

In above command, sed command is used to search for lines that start and end with nothing, which means they are blank lines. '/^$/d' command tells sed to delete those lines. inputfile and outputfile are names of input and output files, respectively.

Example

Let's say we have a file named "sample.txt" that contains following content −

This is first line.

This is second line.

This is fourth line.

To remove blank lines from this file using sed command, we can run following command −

sed '/^$/d' sample.txt > output.txt

This command will create a new file named "output.txt" that contains following content −

This is first line.
This is second line.
This is fourth line.

Method 2: Using grep Command

The grep command is another powerful text manipulation tool that can be used to remove blank lines from a file. Here's syntax to remove blank lines from a file using grep command −

grep -v '^$' inputfile > outputfile

In above command, -v option tells grep to select all lines that do not match given pattern. '^$' pattern matches lines that start and end with nothing, which means they are blank lines. inputfile and outputfile are names of input and output files, respectively.

Example

Let's say we have a file named "sample.txt" that contains following content −

This is first line.

This is second line.

This is fourth line.

To remove blank lines from this file using grep command, we can run following command −

grep -v '^$' sample.txt > output.txt

This command will create a new file named "output.txt" that contains following content −

This is first line.
This is second line.
This is fourth line.

Method 3: Using awk Command

The awk command is a versatile text processing tool that can be used to remove blank lines from a file. Here's syntax to remove blank lines from a file using awk command −

awk '!/^$/' inputfile > outputfile

In above command, ! symbol negates pattern that follows it. /^$/ pattern matches lines that start and end with nothing, which means they are blank lines. inputfile and outputfile are names of input and output files, respectively.

Example

Let's say we have a file named "sample.txt" that contains following content −

This is first line.

This is second line.

This is fourth line.

To remove blank lines from this file using awk command, we can run following command −

awk '!/^$/' sample.txt > output.txt

This command will create a new file named "output.txt" that contains following content −

This is first line.
This is second line.
This is fourth line.

Method 4: Using tr Command

The tr command is a simple text manipulation tool that can be used to remove blank lines from a file. Here's syntax to remove blank lines from a file using tr command −

tr -s '
' < inputfile > outputfile

In above command, -s option tells tr to squeeze multiple newlines into a single newline.
character represents a newline. inputfile and outputfile are names of input and output files, respectively.

Example

Let's say we have a file named "sample.txt" that contains following content −

This is first line.

This is second line.

This is fourth line.

To remove blank lines from this file using tr command, we can run following command −

tr -s '
' < sample.txt > output.txt

This command will create a new file named "output.txt" that contains following content −

This is first line.
This is second line.
This is fourth line.

Method 5: Using Perl Command

The Perl command is a powerful text manipulation tool that can be used to remove blank lines from a file. Here's syntax to remove blank lines from a file using Perl command −

perl -ne 'print if /\S/' inputfile > outputfile

In above command, -ne options tell Perl to loop through lines of inputfile and execute code that follows. print if /\S/ command tells Perl to print line if it contains any non-whitespace character. inputfile and outputfile are names of input and output files, respectively.

Example

Let's say we have a file named "sample.txt" that contains following content −

This is first line.

This is second line.

This is fourth line.

To remove blank lines from this file using Perl command, we can run following command −

perl -ne 'print if /\S/' sample.txt > output.txt

This command will create a new file named "output.txt" that contains following content −

This is first line.
This is second line.
This is fourth line.

Conclusion

In this article, we discussed different methods to remove blank lines from a file in Linux. We discussed sed, grep, awk, tr, and Perl commands and provided examples for each method. These methods can help make files easier to read, reduce file size, and ensure that scripts or programs receive expected input formats. Choose method that works best for your needs and start removing those blank lines from your files today.

Updated on: 24-Mar-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements