Howto delete empty lines using sed command under Linux / UNIX

by Vivek Gite on May 16, 2007 · 24 comments

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.

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 24 comments… read them below or add one }

1 ifrance August 21, 2007

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

Reply

2 diego December 17, 2007

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

Reply

3 vivek December 17, 2007

diego,

Thanks for the heads up.

Reply

4 reymond February 20, 2008

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

Reply

5 Maddy May 28, 2008

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.

Reply

6 Lokesh June 19, 2008

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.

Reply

7 Mitch July 12, 2008

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

Reply

8 joy September 24, 2008

Hi,

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

Reply

9 Vinu October 8, 2008

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.

Reply

10 vivek October 8, 2008

It should work. Are you using GNU sed?

Reply

11 Vinu October 8, 2008

No. It is not working.

Reply

12 cymkhat January 8, 2009

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

Reply

13 Steve January 29, 2009

Thanks cymkat, that worked great for me.

Reply

14 Rajesh Kumar Matcha February 19, 2009

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

Thanks
Rajesh Matcha

Reply

15 om singh October 10, 2009

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

Reply

16 bipin November 9, 2009

thanks for the help
i lilke this type of help

Reply

17 Maya January 8, 2010

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.

Reply

18 swetha February 18, 2010

Thanks a lot :)

Reply

19 Cire April 16, 2010

Thank you very much and God bless you richly!

Reply

20 niro May 1, 2010

how can i rename files which contains spaces??

thanks.

Reply

21 Cire May 3, 2010

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

Reply

22 niro May 4, 2010

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

thanks!

btw, I just LOVE your site!

Reply

23 shaukat April 6, 2011

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

Reply

24 shaukat April 6, 2011

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

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 3 + 6 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: