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
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:
Updated for accuracy.