How to delete empty lines using sed command under Linux / UNIX

I need to delete all empty lines but could not figure out sed command for the same. How do I delete all empty lines with sed under Linux or Unix-like systems?

sed is a stream editor and perfect for this kind of work. You need to use d command under sed which act as the delete function.

ADVERTISEMENTS

Sed Delete Empty Line Syntax

The syntax is as follows to delete empty lines using sed:
sed '/^$/d' <input-file>
echo "LINE" | sed '/^$/d'
echo "$VAR" | sed '/^$/d'
sed '/^[[:space:]]*$/d' input_file

Examples

So to delete all empty lines from a file called /tmp/data.txt, enter:
$ sed '/^$/d' /tmp/data.txt
To store output to another file use redirection operator:
$ sed '/^$/d' /tmp/data.txt > /tmp/output.txt
OR update the file using -i option:
$ sed -i '/^$/d' /tmp/data.txt

Removing all whitespace only using sed

Consider the following file:
more /tmp/data.txt
Sample outputs:

This is a test

foo

bar



nixCraft


Linux and Unix

To delete all empty lines, run:
sed '/^[[:space:]]*$/d' input.file
sed -i '/^[[:space:]]*$/d' input.file
sed -r '/^\s*$/d' input.file > output.file
sed -i '/^[[:space:]]*$/d' /tmp/data.txt

Verify with the help of cat command:
cat output.file
cat /tmp/data.txt

Linux and Unix empty delete empty lines using sed

Deleting a line that matches a pattern

You can also match a word or a pattern to delete. For example
$ cat data.txt
Output:

This is a test
Linux rulez
Windows sucks
Redhat is good server disro

To delete all lines that contain a ‘Windows’ word, enter:
$ sed '/Windows/d' /tmp/data.txt > /tmp/output.data.txt
GNU Sed support -i option to edit files in place:
$ sed -i '/Windows/d' /tmp/data.txt

A note about awk

The awk command offers a much simpler solution as compared to sed command. In this example, remove all empty lines using awk:
awk 'NF' my_input > my_output
cat my_output

Conclusion

In short, use any one of the following sed syntax to remove all empty lines from a text file under Linux, macOS, *BSD and Unix-like systems:
sed '/^[[:space:]]*$/d' input > output
sed '/^\s*$/d' in > out
sed '/^$/d' input > output
sed -n '/^\s*$/!p' in.txt > out.txt

See more sed examples from our page:

šŸ§ Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
30 comments… add one
  • ifrance Aug 21, 2007 @ 20:39

    thanks for your work, I’ll come back again and again,
    I do love unix as well.

  • diego Dec 17, 2007 @ 17:47

    replace:

    $ sed ‘/Windows/d’ /tmp/data.txt

    with

    $ sed -i ‘/Windows/d’ /tmp/data.txt

    Otherwise the command will take no effect on the file /tmp/data.txt, just will dump on stdout.

    $ man sed

  • šŸ§ nixCraft Dec 17, 2007 @ 18:53

    diego,

    Thanks for the heads up.

  • reymond Feb 20, 2008 @ 21:13

    this is my first time using this website. I will us it more frequently.

  • Maddy May 28, 2008 @ 6:21

    This command
    $ sed ‘/^$/d’ /tmp/data.txt > /tmp/output.txt
    is deleting the last line entry from data file.
    for e.g.
    data.txt
    1

    2
    3

    4

    5
    the output is
    1
    2
    3
    4

    last line entry is not there.

  • Lokesh Jun 19, 2008 @ 10:36

    Actually here you might have stopped writting the file after entering 5. You need to press enter button then stop using ^z. Then you will have the result without any data loss.

  • Mitch Jul 12, 2008 @ 17:34

    awesome, your the first to hit it. I don’t know why this was taking me so long. You’ve been bookmark’d

  • joy Sep 24, 2008 @ 7:59

    Hi,

    can you explain what is the use of ^$.
    Thanks You In advance.

  • Vinu Oct 8, 2008 @ 7:10

    The following command works only for null rows, but not in case of spaces:
    sed ‘/^$/d’ filename.
    For eg:
    My record looks like this
    1234

    5678

    91011
    Here after 1234 it is space and so it remains. But after 5678 it is null row and so it is removed. so the result will be
    1234

    5678
    91011.

    Plz tell me what shall I do to eliminate spaces.

  • šŸ§ nixCraft Oct 8, 2008 @ 7:34

    It should work. Are you using GNU sed?

  • Vinu Oct 8, 2008 @ 8:52

    No. It is not working.

  • cymkhat Jan 8, 2009 @ 14:59

    A better way (taking into account, if there are any white-spaces (spaces/tabs)) to delete blank lines would be sed -i ‘/^[ \t]*$/d’ input.txt

  • Steve Jan 29, 2009 @ 5:28

    Thanks cymkat, that worked great for me.

  • Rajesh Kumar Matcha Feb 19, 2009 @ 0:09

    great stuff here . I like the web site . For all my FAQ’s i will come here .

    Thanks
    Rajesh Matcha

  • om singh Oct 10, 2009 @ 18:37

    hi
    How we can replace the data from line2? using the sed command?

  • bipin Nov 9, 2009 @ 19:10

    thanks for the help
    i lilke this type of help

  • Maya Jan 8, 2010 @ 14:07

    I have the same problem what Maddy reported above. My last line is getting deleted when i am trying to delete blank lines using sed command. I cannot amend the file since it is process generated but have to remove the blank lines once the process finishes writing into the file. Please help.

  • swetha Feb 18, 2010 @ 11:47

    Thanks a lot :)

  • Cire Apr 16, 2010 @ 6:02

    Thank you very much and God bless you richly!

  • niro May 1, 2010 @ 11:56

    how can i rename files which contains spaces??

    thanks.

    • eswar May 16, 2011 @ 7:18

      threw specifying double codes we can solve this pbm

  • Cire May 3, 2010 @ 6:14

    niro, do you mean rename files whose names contain spaces?

  • niro May 4, 2010 @ 7:41

    I managed.. I’ll post the script later for anyone whois interested :)

    thanks!

    btw, I just LOVE your site!

  • shaukat Apr 6, 2011 @ 5:35

    Dear friend,

    I have a problem of comparing two files: e.g.
    file1 starts here:
    computer libraray
    books
    fiction
    case study
    group study
    financial crisis

    file 2 starts here:
    case
    crisis
    computer

    I want to compare two files and want to delete lines from file1 which comntains entries in file2. i am using the script as followed but getting error. any help appreciated

    result should be like,

    books
    fiction
    group study

    #!/bin/sh

    while read line1
    do
    while read line2

    do
    sed ‘/$line1/d’ $file2.txt
    done <file2.txt
    file2= $cdr02-05.txt

    done result.txt

  • shaukat Apr 6, 2011 @ 5:43

    I did not write the code which i used:

    #!/bin/sh

    while read line1
    do
    while read line2

    do
    sed ā€˜/$line1/dā€™ $file2.txt
    done <file2.txt
    file2.txt= $file2.txt

    done result.txt

  • Gaurav May 3, 2012 @ 15:50

    Hi
    Please can u provide a command to delete a character from the whole file.
    for ex – If I want to remove # from a file abc.txt.
    Thanks

  • OldManLinuz Jul 31, 2012 @ 17:03

    Gaurav,
    you can do it two ways cat | grep -v “#” >> newfile.txt;

    Or you can cat | sed -e s!#!!g >> newfile.txt

    Or you can cat | sed -e s/#//g >> newfile.txt.

    Hope that helps.

  • mayank Jun 25, 2013 @ 10:13

    Hi,

    Can any1 help in getting the desired output.

    My data is like

    1 A
    2 B
    3 C

    4 D
    5 E
    6 F

    7 G
    8 H
    9 I

    i WANT THE OUT PUT LIKE

    1 A 4 D 7 G
    2 B 5 E 8 H
    3 C 6 F 9 I

    THANKS IN ADVANCE!!

  • sugatang itlog Aug 13, 2013 @ 0:10
    sed -e 's/#.*//;/^\s*$/d' /etc/lvm/lvm.conf

    , only shows the lines without #

  • shrikant Sep 2, 2017 @ 20:19

    Hi, I have an issue, please help:
    input.txt file
    ~1
    001
    81648736247637
    common
    7483748*********
    –3space
    836546475764
    0034
    +87485
    –2Spaces
    985467
    ~1
    ~2
    001
    93845439867534
    comon
    623847632846*******
    347584737
    –2spaces
    8675467
    00234
    +19381234
    —3spaces
    ~2

    I want to print each record including spaces between ~1 to ~1, ~2 to ~2 …so on into rows for each record start with ~ and ends with next ~.
    ex :
    output.txt
    00181648736247637common7483748********* —3space836546475764
    0034+87485–2Spaces985467 …as my first row
    00193845439867534comon623847632846*******347584737–2spaces867546700234+19381234—3spaces ….as my second row and so on.
    Could you pls help..
    Thanks in advance

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.