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
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
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!
Tags: sed delete, sed delete lines, sed find and replace, sed remove blank lines, sed replace, sed replace string, sed search and replace, stream editor, syntax



August 21st, 2007 at 8:39 pm
thanks for your work, I’ll come back again and again,
I do love unix as well.
December 17th, 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
December 17th, 2007 at 6:53 pm
diego,
Thanks for the heads up.
February 20th, 2008 at 9:13 pm
this is my first time using this website. I will us it more frequently.
May 28th, 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.
June 19th, 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.
July 12th, 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
September 24th, 2008 at 7:59 am
Hi,
can you explain what is the use of ^$.
Thanks You In advance.
October 8th, 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.
October 8th, 2008 at 7:34 am
It should work. Are you using GNU sed?
October 8th, 2008 at 8:52 am
No. It is not working.