How to delete empty lines using sed command under Linux / UNIX

www.la‮iruttu‬.com
How to delete empty lines using sed command under Linux / UNIX

To delete empty lines using the sed command in Linux or Unix, you can use the d command with the /^$/ pattern. The /^$/ pattern matches lines that are empty, and the d command tells sed to delete them.

Here is an example of how to use the sed command to delete empty lines:

sed '/^$/d' input.txt > output.txt

This will read the file input.txt, delete any empty lines, and write the resulting output to the file output.txt.

You can also use the sed command to delete empty lines from the standard input, like this:

cat input.txt | sed '/^$/d'

This will read the contents of input.txt and pipe it to the sed command, which will delete any empty lines and print the resulting output to the standard output.

Note that the sed command works by making a copy of the input, performing the specified actions on the copy, and then printing the copy to the output. It does not modify the input file itself. If you want to modify the input file, you can use the -i option to tell sed to edit the file in place. For example:

sed -i '/^$/d' input.txt

This will delete any empty lines from the file input.txt and save the changes to the file.

Created Time:2017-10-28 21:39:02  Author:lautturi