Categories
unix

Deleting blank lines in a file using UNIX

In this post, let us learn about deleting blank lines in a file using UNIX.

Creating File with blank lines

I am having one file with the following as the content and also some blank/empty lines in it.

cat abc.txt
apple
Mango
Orange

Grapes

Peer

Pineapple

Using the below-given command, we can get to know the count of the line which includes the blank lines also.

wc -l abc.txt
9 abc.txt

This command lists the number of blank lines in a file.

^ – beginning of the line

$ – ending of the line

grep -c ^$ abc.txt
3

sed command helps us to delete the empty lines by checking for the matches.

sed -i '/^$/d' abc.txt

After the deletion, the File in the UNIX looks like below.

cat abc.txt
apple
Mango
Orange
Grapes
Peer
Pineapple

The count of the file after deleting the blank lines in a file using UNIX.

cat abc.txt |wc -l
6