About Linux FAQ

Browse More FAQs:

Howto delete empty lines using sed command under Linux / UNIX

Posted by Vivek Gite [Last updated: December 17, 2007]

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

See more sed examples:

=> Delete text or paragraph between two sections using sed

Updated for accuracy.

E-mail    Print    Can't find an answer to your question? Contact us

Related Other Helpful FAQs:

Discussion on This FAQ

  1. ifrance Says:

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

  2. diego Says:

    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

  3. vivek Says:

    diego,

    Thanks for the heads up.

  4. reymond Says:

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

  5. Maddy Says:

    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.

  6. Lokesh Says:

    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.

  7. Mitch Says:

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

  8. joy Says:

    Hi,

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

  9. Vinu Says:

    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.

  10. vivek Says:

    It should work. Are you using GNU sed?

  11. Vinu Says:

    No. It is not working.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags: , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Powered by Open source software.