≡ Menu

Howto delete empty lines using sed command under Linux / UNIX

Q. 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?

A. sed is a stream editor and perfect for these kind of work.

You need to use d command under sed which is act as the delete function.

Sed Delete Empty Line Syntax

sed ‘/^$/d’ <input-file>
echo LINE | sed ‘/^$/d’
echo $VAR | sed ‘/^$/d’

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

Deleting a line that matches a pattern

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

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

See more sed examples:

=> Delete text or paragraph between two sections using sed

Updated for accuracy.

Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 29 comments… add one }
  • ifrance August 21, 2007, 8:39 pm

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

  • diego December 17, 2007, 5:47 pm


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


    $ 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 December 17, 2007, 6:53 pm


    Thanks for the heads up.

  • reymond February 20, 2008, 9:13 pm

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

  • Maddy May 28, 2008, 6:21 am

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



    the output is

    last line entry is not there.

  • Lokesh June 19, 2008, 10:36 am

    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 July 12, 2008, 5:34 pm

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

  • joy September 24, 2008, 7:59 am


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

  • Vinu October 8, 2008, 7:10 am

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


    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


    Plz tell me what shall I do to eliminate spaces.

  • nixCraft October 8, 2008, 7:34 am

    It should work. Are you using GNU sed?

  • Vinu October 8, 2008, 8:52 am

    No. It is not working.

  • cymkhat January 8, 2009, 2:59 pm

    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 January 29, 2009, 5:28 am

    Thanks cymkat, that worked great for me.

  • Rajesh Kumar Matcha February 19, 2009, 12:09 am

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

    Rajesh Matcha

  • om singh October 10, 2009, 6:37 pm

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

  • bipin November 9, 2009, 7:10 pm

    thanks for the help
    i lilke this type of help

  • Maya January 8, 2010, 2:07 pm

    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 February 18, 2010, 11:47 am

    Thanks a lot :)

  • Cire April 16, 2010, 6:02 am

    Thank you very much and God bless you richly!

  • niro May 1, 2010, 11:56 am

    how can i rename files which contains spaces??


    • eswar May 16, 2011, 7:18 am

      threw specifying double codes we can solve this pbm

  • Cire May 3, 2010, 6:14 am

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

  • niro May 4, 2010, 7:41 am

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


    btw, I just LOVE your site!

  • shaukat April 6, 2011, 5:35 am

    Dear friend,

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

    file 2 starts here:

    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,

    group study


    while read line1
    while read line2

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

    done result.txt

  • shaukat April 6, 2011, 5:43 am

    I did not write the code which i used:


    while read line1
    while read line2

    sed ‘/$line1/d’ $file2.txt
    done <file2.txt
    file2.txt= $file2.txt

    done result.txt

  • Gaurav May 3, 2012, 3:50 pm

    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.

  • OldManLinuz July 31, 2012, 5:03 pm

    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 June 25, 2013, 10:13 am


    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


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


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

    , only shows the lines without #

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , ,