Bash (Scripting) - do something empty lines (sed)

by
Jeremy Canfield |
Updated: April 16 2021
| Bash (Scripting) articles
Let's say file.txt contains empty lines.
line 1
line 3
line 5
line 7
The following sed command will remove the empty lines. The regular expression looks for the beginning (^) of a line immediately followed by the end ($) of a line, and if matched, deletes (d) the line.
~]# sed '/^$/d' file.txt
line 1
line 3
line 5
line 7
Remember that the -i option must be used to actually perform this change on the file.
~]# sed -i '/^$/d' file.txt
line 1
line 3
line 5
line 7
Did you find this article helpful?
If so, consider buying me a coffee over at