Table of Contents

BASH - Files - Delete empty lines

Using awk

awk 'NF' my_input > my_output

Using sed

Delete empty lines using sed.

sed '/^$/d' <input-file>
echo "LINE" | sed '/^$/d'
echo "$VAR" | sed '/^$/d'
sed '/^[[:space:]]*$/d' input_file
 
sed '/^$/d' input > output
sed '/^\s*$/d' in > out
sed -n '/^\s*$/!p' in.txt > out.txt
sed '/^[[:space:]]*$/d' input > output

Examples

Delete all empty lines from a file

sed '/^$/d' /tmp/data.txt

Store output to another file use redirection operator

sed '/^$/d' /tmp/data.txt > /tmp/output.txt

Update the file directly

sed -i '/^$/d' /tmp/data.txt