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.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop









![Linux / Unix: Sed Substitute Multiple Patterns [ Find & Replace ]](http://s13.cyberciti.org/images/shared/rp/3/29.jpg)




{ 26 comments… read them below or add one }
thanks for your work, I’ll come back again and again,
I do love unix as well.
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
diego,
Thanks for the heads up.
this is my first time using this website. I will us it more frequently.
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.
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.
awesome, your the first to hit it. I don’t know why this was taking me so long. You’ve been bookmark’d
Hi,
can you explain what is the use of ^$.
Thanks You In advance.
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.
It should work. Are you using GNU sed?
No. It is not working.
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
Thanks cymkat, that worked great for me.
great stuff here . I like the web site . For all my FAQ’s i will come here .
Thanks
Rajesh Matcha
hi
How we can replace the data from line2? using the sed command?
thanks for the help
i lilke this type of help
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.
Thanks a lot :)
Thank you very much and God bless you richly!
how can i rename files which contains spaces??
thanks.
niro, do you mean rename files whose names contain spaces?
I managed.. I’ll post the script later for anyone whois interested :)
thanks!
btw, I just LOVE your site!
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
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
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
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.