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' filenameWarning: This will delete all blank lines AND lines containing only whitespace
GNU sed
sed -i '/^$/d' filenameThis 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 > newfilenameGNU grep
grep -v '^$' filename > newfilenameThis 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:
Post a Comment