Howto delete empty lines using sed command under Linux / UNIX

by on May 16, 2007 · 29 comments· LAST UPDATED December 17, 2007

in , ,

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.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 29 comments… read them below or add one }

1 ifrance August 21, 2007 at 8:39 pm

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

Reply

2 diego December 17, 2007 at 5:47 pm

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

diego,

Thanks for the heads up.

Reply

4 reymond February 20, 2008 at 9:13 pm

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

Reply

5 Maddy May 28, 2008 at 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.
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 at 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.

Reply

7 Mitch July 12, 2008 at 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

Reply

8 joy September 24, 2008 at 7:59 am

Hi,

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

Reply

9 Vinu October 8, 2008 at 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
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 nixCraft October 8, 2008 at 7:34 am

It should work. Are you using GNU sed?

Reply

11 Vinu October 8, 2008 at 8:52 am

No. It is not working.

Reply

12 cymkhat January 8, 2009 at 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

Reply

13 Steve January 29, 2009 at 5:28 am

Thanks cymkat, that worked great for me.

Reply

14 Rajesh Kumar Matcha February 19, 2009 at 12:09 am

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 at 6:37 pm

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

Reply

16 bipin November 9, 2009 at 7:10 pm

thanks for the help
i lilke this type of help

Reply

17 Maya January 8, 2010 at 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.

Reply

18 swetha February 18, 2010 at 11:47 am

Thanks a lot :)

Reply

19 Cire April 16, 2010 at 6:02 am

Thank you very much and God bless you richly!

Reply

20 niro May 1, 2010 at 11:56 am

how can i rename files which contains spaces??

thanks.

Reply

21 eswar May 16, 2011 at 7:18 am

threw specifying double codes we can solve this pbm

Reply

22 Cire May 3, 2010 at 6:14 am

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

Reply

23 niro May 4, 2010 at 7:41 am

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

thanks!

btw, I just LOVE your site!

Reply

24 shaukat April 6, 2011 at 5:35 am

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

25 shaukat April 6, 2011 at 5:43 am

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

26 Gaurav May 3, 2012 at 3:50 pm

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

Reply

27 OldManLinuz July 31, 2012 at 5:03 pm

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.

Reply

28 mayank June 25, 2013 at 10:13 am

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

Reply

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

, only shows the lines without #

Reply

Leave a Comment

Tagged as: , , , , , , , ,

Previous Faq:

Next Faq: