To remove empty lines from a file in a Unix or Linux shell, you can use the sed
command with the /^$/d
regular expression.
The /^$/
regular expression matches lines that contain nothing but whitespace characters, including the empty line. The d
command tells sed
to delete these lines.
For example, to remove empty lines from the file input.txt
and save the result to the file output.txt
, you can use the following command:
sed '/^$/d' input.txt > output.txt
This will remove all empty lines from the input.txt
file and save the modified output to the output.txt
file.
You can also use the grep
command with the -v
option to remove empty lines. For example, to remove empty lines from the file input.txt
and save the result to the file output.txt
using grep
, you can use the following command:
grep -v '^$' input.txt > output.txt
This will remove all lines from the input.txt
file that contain nothing but whitespace characters, and save the modified output to the output.txt
file.
It is important to note that the sed
and grep
commands operate on individual lines, and will not remove lines that contain only whitespace characters, but are not completely empty. To remove these lines as well, you may need to use additional options or regular expressions.