April 16, 2008

Multiple Ways to Delete Blank Lines From a Text File

You have a text file that has a bunch of blank lines in it. You want to do some operation on that text file but first want to delete the blank lines. You could open the file in vi and manually delete them all but that isn't the Unix way. Here are a number of easy ways to delete blank lines.

If you already have the text file open in vi/vim, the following will delete all blank lines while in command mode:
:g/^$/d

Here are a few different options from the shell using standard GNU tools:

GNU awk
awk '/./' filename
awk 'NF > 0' filename
Warning: This will delete all blank lines AND lines containing only whitespace

GNU sed
sed -i '/^$/d' filename
This makes use of the regular expression ^$ representing blank lines. The -i flag tells sed to edit the file in place. If you want to preserve the original file, simple remove the -i and redirect the output to a new file:
sed '/^$/d' filename > newfilename
GNU grep
grep -v '^$' filename > newfilename
This is the same as sed with the use of the regex ^$

Of course there are many other ways to accomplish this task. That is the beauty of Unix.

No comments: