How to remove empty lines from file in Unix

Introduction

Removing empty lines in a text file from the Unix shell is quite easy. There are a couple of ways to do that. Let us explore a few…

Remove empty lines using sed command

The stream editor (sed) can be used to filter and transform text. Removing empty lines using sed can be done using a regular expression and the (d) option.

Remove empty lines using grep command

Grep command can be used to search for a text pattern. We can utilize that and search for non empty lines and direct the output to a file or the console.

Remove empty lines using awk command

NF in awk evaluates to the number of fields in the line. If the line is not empty, NF is greater than zero which triggers the default awk action (i.e printing the whole line)

References

Thanks for visiting

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *