Several methods of deleting all empty lines in text under Linux

Method 1: Use Grep
grep -v ‘^\s*$’ test.txt
Note: -v means to reverse the matching result and the regular expression matches the blank line. (Blank lines can include whitespace characters such as space TAB characters)
Method 2: Use SED
sed ‘/^\s*$/d’ test.txt
Note: D stands for delete the line
Method 3: Use AWK
awk NF test.txt
Note: NF represents the number of fields in the current row. If the line is blank, the number of fields is 0, which is interpreted as false by AWK, so it will not be output.
 
All three of these methods can handle blank lines containing whitespace characters (Spaces, tabs, etc.).
 
Method 4: If blank lines are caused by ‘\n’, you can also use the tr command to remove them
tr -s ‘\n’ < test.txt
Note: -s represents the compression of multiple consecutive characters into a character. In this case, multiple ‘\n’ is compressed into a ‘\n’ to remove empty lines.
A weakness of method 4: If the first line is empty, the first line cannot be removed
The level is limited, if has the improper place, also hoped corrects!

Read More: