How to remove empty lines from a file in bash shell linux ?

Today in this post, we will look how to remove empty lines from a file in bash shell linux.

This is the easiest way to achieve this task.

Removing empty/null lines

### Create a sample file with spaces in between.
[root@nglinux ]# cat testfile
## hello, this is comment
## welcome to ngelinux

Tomatoes are 10$
Onion is 15$

### Grep . which greps the line with text and avoids any line
### with newline i.e. \n character.
[root@nglinux ]# cat testfile | grep .
## hello, this is comment
## welcome to ngelinux
Tomatoes are 10$
Onion is 15$
[root@nglinux ]#

Hence, grep . will remove all newline or empty lines and hence we have only those lines in output which has some text.

Leave a Reply

avatar
  Subscribe  
Notify of