Linux – How to remove empty/blank lines from a file in Unix (including spaces)

command-line-interfacelinuxparsingtext;unix

How do I remove empty/blank (including spaces only) lines in a file in Unix/Linux using the command line?

contents of file.txt

Line:Text
1:<blank>
2:AAA
3:<blank>
4:BBB
5:<blank>
6:<space><space><space>CCC
7:<space><space>
8:DDD

output desired

1:AAA
2:BBB
3:<space><space><space>CCC
4:DDD

Best Answer

This sed line should do the trick:

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

The -i means it will edit the file in-place.