How to Remove Blank Lines from a file in Linux

Sometimes you may want to remove or delete blank lines from a file on Linux. This can be done in many ways, but in this article we have listed best five methods.

You already know the grep, awk and sed commands that specialize in text data manipulation and we will use that command to remove blank lines in Linux.

These are included in advanced commands category because they are often used in shell script.

In this guide, we’ll see how to remove blank lines from a file in Linux using the following five methods.

  • sed Command: Stream editor for filtering and transforming text.
  • grep Command: Print lines that match patterns.
  • cat Command: It concatenate files and print on the standard output.
  • tr Command: Translate or delete characters.
  • awk Command: The awk utility shall execute programs written in the awk programming language, which is specialized for textual data manipulation.
  • perl Command: Perl is a programming language specially designed for text editing.

To prove this, I have already created the '2daygeek.txt' file with some texts and blank lines, which are shown below:

Now, all the prerequisites have been made, we are going to test this in several ways.

cat 2daygeek.txt

2daygeek.com is a best Linux blog to learn Linux.

This is an EIGHT years old blog.

This website is maintained by Magesh M and is licensed under CC BY-NC 4.0.

He had two daughters.

Their names are Tanisha & Renusha.

1) Removing Empty Lines from a file using sed Command

sed is a stream editor used to perform basic text transformations, which can also be used for removing empty lines from a file as shown below:

sed -i '/^$/d' 2daygeek.txt

2daygeek.com is a best Linux blog to learn Linux.
This is an EIGHT years old blog.
This website is maintained by Magesh M and is licensed under CC BY-NC 4.0.
He had two daughters.
Their names are Tanisha & Renusha.
Removing Blank Lines from a file in Linux

Details are follow:

  • sed: It’s a command
  • //: It holds the searching string.
  • ^: Matches start of string.
  • $: Matches end of string.
  • d: Delete the matched string.
  • 2daygeek.txt: Source file name.

2) Deleting Blank Lines from a file using grep Command

grep stands for Global Regular Expression Print. It is used to search text and strings in a given file and prints each line that matches a pattern.

$ grep . 2daygeek.txt > new_file.txt
or
$ grep -Ev "^$" 2daygeek.txt > new_file.txt

2daygeek.com is a best Linux blog to learn Linux.
This is an EIGHT years old blog.
This website is maintained by Magesh M and is licensed under CC BY-NC 4.0.
He had two daughters.
Their names are Tanisha & Renusha.

Details are follow:

  • grep: It’s a command
  • .: Replaces any character.
  • ^: matches start of string.
  • $: matches end of string.
  • E: For extended regular expressions pattern matching.
  • e: For regular expressions pattern matching.
  • v: To select non-matching lines from the file.
  • 2daygeek.txt: Source file name.

3) Removing Empty Lines from a file in Linux using awk Command

Awk is a general-purpose scripting language designed for advanced textual data processing. It’s mostly used for text manipulation, reporting and analysis.

awk NF 2daygeek.txt > new_file.txt
or
awk '!/^$/' 2daygeek.txt > new_file.txt
or
awk '/./' 2daygeek.txt > new_file.txt

2daygeek.com is a best Linux blog to learn Linux.
This is an EIGHT years old blog.
This website is maintained by Magesh M and is licensed under CC BY-NC 4.0.
He had two daughters.
Their names are Tanisha & Renusha.

Details are follow:

  • awk: It’s a command
  • //: It holds the searching string.
  • ^: matches start of string.
  • $: matches end of string.
  • .: Replaces any character.
  • !: Delete the matched string.
  • 2daygeek.txt: Source file name.

4) Delete Blank Lines from a file using cat and tr Command

cat stands for concatenate. It is very frequently used in Linux to reads data from a file.

cat is one of the most frequently used commands on Unix-like operating systems. It offers three functions which is related to text file such as display content of a file, combine multiple files into the single output and create a new file.

We can easily remove blank lines in a file by combining ‘cat’ and ‘tr’ command as shown below:

cat 2daygeek.txt | tr -s '\n' > new_file.txt

2daygeek.com is a best Linux blog to learn Linux.
This is an EIGHT years old blog.
This website is maintained by Magesh M and is licensed under CC BY-NC 4.0.
He had two daughters.
Their names are Tanisha & Renusha.

Details are follow:

  • cat: It’s a command
  • tr: It’s a command
  • |: Pipe symbol. It pass first command output as a input to another command.
  • s: Replace each sequence of a repeated character that is listed in the last specified SET.
  • \n: To add a new line.
  • 2daygeek.txt: Source file name.

5) Remove Empty Lines from a file in Linux using  perl Command

Perl stands for Practical Extraction and Reporting Language. Perl is a programming language specially designed for text editing. It is now widely used for a variety of purposes including Linux system administration, network programming, web development, etc.

perl -ne 'print if /\S/' 2daygeek.txt  > new_file.txt

2daygeek.com is a best Linux blog to learn Linux.
This is an EIGHT years old blog.
This website is maintained by Magesh M and is licensed under CC BY-NC 4.0.
He had two daughters.
Their names are Tanisha & Renusha.

Details are follow:

  • grep: It’s a command
  • .: Replaces any character.
  • ^: matches start of string.
  • $: matches end of string.
  • E: For extended regular expressions pattern matching.
  • e: For regular expressions pattern matching.
  • v: To select non-matching lines from the file.
  • 2daygeek.txt: Source file name.

Closing Notes

In this guide, we’ve shown you how to remove empty/blank lines from a file in Linux.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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