You can use commands like sed, awk, or grep to delete all empty lines from a file in Linux.
Example:
Create a file with multiple empty lines:
1 2 3 4 5 6 7 8 9 10 |
## Create a file with multiple empty lines cat << EOF > myfile Hello everyone Welcome to CloudAffaire This is Debjeet! EOF |
Using sed:
1 2 3 4 5 6 7 8 9 |
## replace the content sed -i '/^[[:space:]]*$/d' myfile #or sed -i '/^\s*$/d' myfile #or sed -i '/^$/d' myfile # without changing the content sed -n '/^\s*$/!p' myfile |
Using awk:
1 2 3 4 5 6 7 8 9 10 |
# without changing the content awk /./ myfile #or awk 'NF' myfile #or awk 'length' myfile #or awk '/^[ \t]*$/ {next;} {print}' myfile #or awk '!/^[ \t]*$/' myfile |
Using grep:
1 2 3 4 5 |
# without changing the content grep . myfile grep -v '^$' myfile grep -v '^\s*$' myfile grep -v '^[[:space:]]*$' myfile |
Check the content of the file:
1 2 |
## Get the content of the file cat myfile |