HowTo: Remove Blank Lines From a File

Use one of the following commands to remove blank lines from a file.

1. Using the grep command:

$ grep -v "^$" file.txt

2. Using the sed command:

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

3. Using the awk command:

$ awk '/./' file.txt

4. Using the tr command:

$ tr -s '\n' < file.txt

You also can pipe the output of each command to a new file, like follows:

$ grep -v "^$" input.txt > output.txt
Was it useful? Share this post with the world!

Leave a Reply