Linux remove empty line

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

  • 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.
Читайте также:  Настройки шлюза linux debian

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.
  • 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.
  • 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.
  • 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.
Читайте также:  Linux подключить сетевой диск ntfs

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.
  • 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.

Источник

How to Delete Empty Lines in Files Using Grep, Sed, and Awk

An experienced Linux user knows exactly what kind of a nuisance blank lines can be in a processable file. These empty/blank lines not only get in the way of correctly processing such files but also make it difficult for a running program to read and write the file.

On a Linux operating system environment, it is possible to implement several text manipulation expressions to get rid of these empty/blank lines from a file. In this article, empty/blank lines refer to the whitespace characters.

Create a File with Empty/Blank Lines in Linux

We need to create a reference file with some empty/blank lines. We will later amend it in the article through several techniques we will be discussing. From your terminal, create a text file of your choice with a name like “i_have_blanks” and populate it with some data and some blank spaces.

$ nano i_have_blanks.txt Or $ vi i_have_blanks.txt

Create File with Blank Lines

Throughout the article, we will be outputting the contents of a file on our terminal using the cat command for flexible referencing.

View File Contents in Linux

The three Linux commands that will propel us towards an ideal solution to this empty/blank lines problem are grep, sed, and awk.

Therefore, create three copies of your i_have_blanks.txt file and save them with different names so that each can be accommodated by one of the stated three Linux commands.

Through regex (regular expressions), we can identify blank lines with the POSIX standard character “[:space:]” .

Читайте также:  Узнать характеристики сервера linux

How to Remove Blank/Empty Lines in Files

With this problem statement, we are considering the elimination of all existing empty/blank lines from a given readable file using the following commands.

1. Delete Empty Lines Using Grep Command

Supported use of shorthand character classes can reduce the grep command to a simple one like:

$ grep -v '^[[:space:]]*$' i_have_blanks.txt OR $ grep '\S' i_have_blanks.txt

To fix a file with blank/empty lines, the above output needs to pass through a temp file before overwriting the original file.

$ grep '\S' i_have_blanks.txt > tmp.txt $ mv tmp.txt i_have_blanks.txt $ cat i_have_blanks.txt

Remove Blank Lines in File Using Grep

As you can see, all the blank lines that spaced the content of this text file are gone.

2. Delete Empty Lines Using Sed Command

The d action in the command tells it to delete any existing whitespace from a file. This command’s blank line matching and deleting mechanism can be represented in the following manner.

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

The above command scans through the text file lines for non-blank characters and deletes all the other remaining characters. Through its non-blank character class support, the above command can be simplified to the following:

$ sed '/\S/!d' i_have_blanks_too.txt

Also, because of the command’s in-place editing support, we don’t need a temp file to temporarily hold our converted file before overwriting the original text file like with the case of the grep command. You however need to use this command with an -i option as an argument.

$ sed -i '/\S/!d' i_have_blanks_too.txt i_have_blanks_too.txt $ cat i_have_blanks_too.txt

Remove Blank Lines in File Using Sed

3. Delete Empty Lines Using Awk Command

The awk command runs a non-white character check on each line of a file and only prints them if this condition is true. The flexibility of this command comes with various implementation routes. Its straightforward solution is as follows:

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

The interpretation of the above command is straightforward, only the file lines that do not exist as whitespaces are printed. The longer version of the above command will look similar to the following:

$ awk '< if($0 !~ /^[[:space:]]*$/) >' i_have_blanks_first.txt

Through awk non-blank character class support, the above command can also be represented in the following manner:

$ awk -d '/\S/' i_have_blanks_first.txt

Remove Blank Lines in File Using Awk

The -d an option lets awk dump the final file lines on the system terminal. As you can see, the file no longer has whitespaces.

The three discussed and implemented solutions to dealing with blank lines from files through grep, sed, and awk commands will take us a long way into implementing stable and efficient read and write file operations on a Linux system.

Источник

Оцените статью
Adblock
detector