recoverit.wondershare.com Open in urlscan Pro
2.19.126.153  Public Scan

URL: https://recoverit.wondershare.com/linux-recovery/bash-remove-empty-lines.html
Submission: On January 02 via api from GB — Scanned from GB

Form analysis 0 forms found in the DOM

Text Content

   
 * Video Creativity
   Video Creativity Products
   Filmora
   Complete video editing tool.
   DemoCreator
   Efficient tutorial video maker.
   UniConverter
   High-speed media conversion.
   Virbo
   Powerful AI video generator.
   Presentory
   AI video presentation maker.
   View all products
   Explore
    * Overview
    * Video
    * Photo
    * Creative Center

 * Diagram & Graphics
   Diagram & Graphics Products
   EdrawMax
   Simple diagramming.
   EdrawMind
   Collaborative mind mapping.
   EdrawProj
   A professional Gantt chart tool.
   Mockitt
   Design, prototype & collaborate online.
   View all products
 * PDF Solutions
   PDF Solutions Products
   PDFelement
   PDF creation and editing.
   Document Cloud
   Cloud-based document management.
   PDF Reader
   Simple and free PDF reading.
   HiPDF
   Free All-In-One Online PDF Tool.
   View all products
 * Data Management
   Data Management Products
   Recoverit
   Lost file recovery.
   Repairit
   Repair broken videos, photos, etc.
   Dr.Fone
   Mobile device management.
   MobileTrans
   Phone to phone transfer.
   FamiSafe
   Parental control app.
   View all products
 * Explore AI
 * Business
 * Shop
 * Support
 * Sign in
 * Account Center Sign out

Recoverit
 * Products
   
   
   DATA RECOVERY
   
   
    * Data Recovery for Windows
    * Data Recovery for Mac
    * Data Recovery for Free
   
   
   
   DATA BACKUP
   
   
    * UBackit Data Backup
   
   
   
   DATA REPAIR
   
   
    * Repairit for Desktop AI
    * Repairit Online AI
    * Repairit for Email

 * Features
   
   
   RECOVER MEDIA FILES
   
   
    * Photo Recovery
    * Video Recovery
   
   
   
   RECOVER DOCUMENT FILES
   
   
    * File Recovery
    * Excel Recovery
   
   
   
   RECOVER FROM DEVICES
   
   
    * NAS Recovery
    * Linux Recovery
    * Memory Card Recovery
    * USB Data Recovery
    * Hard Drive Recovery
    * Windows System Recovery
   
   RECOVER FROM DEVICES
   
   
    * USB Data Recovery
    * Hard Drive Recovery
    * Windows System Recovery
   
   CHECK ALL FEATURES
 * Resources
   
   
   FILE SOLUTIONS
   
   
    * Office Document Solutions
    * Photo/Video/Audio/Camera Solutions
    * Email-Related Solutions
   
   
   
   COMPUTER SOLUTIONS
   
   
    * Windows Computer Solutions
    * Mac Computer Solutions
    * Linux Solutions
   
   
   
   STORAGE DEVICE SOLUTIONS
   
   
    * Hard Drive Solutions
    * SD Card Solutions
    * USB Drive Solutions
    * NAS Disk Solutions
   
   
   
   BACKUP SOLUTIONS
   
   
    * Data Backup Solutions
   
   
   
   KNOWLEDGE CENTER
   
   
    * File Format
    * File System
    * Storage Media
    * Disk Parition
   
   FIND MORE SOLUTIONS
 * Guide
 * DOWNLOAD DOWNLOAD
 * Buy Now Buy Now
 * 
   

🔔 Happy New Year! Recoverit's New Year Sale - Save up to 40% OFF! ⏰ Act Fast,
Countdown: 05:16:02:57🎉 Don't Miss the Deal of the Year!

Home > Linux Recovery & Hacks > How To Remove Empty Lines in Bash on Linux Like
a Pro


HOW TO REMOVE EMPTY LINES IN BASH ON LINUX LIKE A PRO



THEO LUCIA



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.

IN THIS ARTICLE

01 6 Methods to Remove Empty Lines in Bash in Linux
02 How To Recover Accidentally Deleted Files in Linux


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

Try It Free Try It Free

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.



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.



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



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.



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


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!

THEO LUCIA




YOU MAY ALSO LIKE

HOW TO REMOVE FILES WITH THE UNLINK COMMAND ON LINUX AND HOW TO RECOVER THEM



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 UNDELETE FILES ON EXT4 FILE SYSTEM



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?

HOT ARTICLES

 * LINUX DATA RECOVERY
   
    * Recover Deleted Files in Linux
    * Top 10 Linux Recovery Tools
    * Recover Deleted Files in Ubuntu
    * Recover Deleted Files by RM Command
    * RAID Recovery Using mdadm
    * Kali Linux Data Recovery
    * Restore Overwritten File on Linux
    * Recover EXT4 Partition
    * Recover Linux Files on Windows
    * SD Card Data Recovery on Linux
    * Undelete Files on XFS File System
    * How To Use Safecopy on Linux
    * EaseUS Alternatives for Linux
    * How To Install & Use Extundelete
    * How To Use Scalpel for Linux
    * Recuva Alternatives for Linux
    * Disk Drill Alternatives for Linux
    * Read XFS Drive on Windows

 * LINUX TIPS & TRICKS
   
    * Dual Boot Windows and Linux
    * Format Disk Partition in Linux
    * Delete Partitions in Linux
    * Recover Deleted Linux Partitions
    * 10 Linux Partition Recovery Tools
    * Access Linux Partitions from Windows
    * How To Resize Partition in Linux
    * How To Use GNU Ddrescue
    * Install and Use TestDisk on Linux
    * How To Use PhotoRec on Linux
    * How To Use Foremost on Linux
    * How To Find Duplicate Files in Linux
    * How To Clear Temp Files in Linux
    * How To Delete Files in Ubuntu




HERO PRODUCTS


 * Filmora
 * UniConverter
 * Recoverit
 * Dr.Fone
 * PDFelement
 * FamiSafe
 * All Products

WONDERSHARE


 * Creative Center
 * About Us
 * Newsroom
 * Global Presence
 * Founder's Speech
 * Careers
 * Education

HELP CENTER


 * Contact Us
 * Video Community
 * Support Center
 * Activation & Registration
 * Account

GROUP MEMBER


 * 
 * 

   FOLLOW US

    * 

Terms and Conditions Privacy Cookies License Agreement Refund Policy Uninstall

Copyright © 2024 Wondershare. All rights reserved. The order process, tax issue,
and invoicing to end users are conducted by Wondershare Technology Co., Ltd,
which is a subsidiary of Wondershare Group.

English

English Arabic - العربية Chinese - 简体中文 Chinese Traditional - 繁體中文 French -
Français German - Deutsch Indonesian - Bahasa Indonesia Italian - Italiano
Japanese - 日本語 Korean - 한국어 Portuguese - Português Spanish - Español


Wondershare Recoverit
Your Ultimate Data Recovery Tool
Effortlessly retrieve lost or deleted files from 2000+ storage devices,
including HDDs, SSDs, external hard drives, USB drives, SD cards, NAS servers,
and more. No matter how you lost your files, our software recovers all types of
data with ease and confidence.
Download Free Now Download Free Now
Security Verified. 7,290,205 people have downloaded it.
Check Your 2023 Data Recovery Annual Report Now! 📧>>