You sometimes need to do plain text processing, and you might need to remove empty lines from a file. As for “empty lines” here we mentioned are the lines that don’t contain either space or table.
For example, if you want to get a summarised output of Apache’s configuration file httpd.conf, you just need to remove all the empty lines and commented-out lines.
There are lots of ways to do it. We will show some typical examples here.
1, Delete empty lines with sed
To delete empty lines, you can run,
sed -i '/^\s*$/d' file
Or you can delete lines that only contains return as this,
sed -i '/^$/d' file
Without the -i parameter, sed will write updated file content in standard output instead of writing to the original file.
2, Delete empty lines (or lines only contains space and table) with awk
awk NF file > new_file
3, With grep to delete empty lines
grep -v '^\s*$' file > new_file