FreeKB - Remove empty lines from a file in Linux bash
Remove empty lines from a file in Linux bash

Home > Search


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


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 732da in the box below so that we can be sure you are a human.




Comments