My experience has been that I have often received files that contain spaces and TABS in otherwise empty lines. I call these visually empty lines. Usually we want to get rid of these as well as lines which have no characters at all.
Here is what I usually use:
#!/usr/bin/env sh
# @(#) dbl Delete blank ( visually empty ) lines.
# If POSIX character class does not work, then use
# [ SPACE TAB ].
grep -v '^[[:space:]]*$' $*
# grep -v '^[ ]*$' $*
exit $?
Here is a demonstration script:
#!/bin/bash -
# @(#) s1 Demonstrate visually empty line processing.
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) cat grep
FILE=${1-data1}
echo
echo " Input file $FILE:"
cat -A $FILE
echo
echo " Processed with grep '.':"
grep . $FILE
echo
echo " Processing with dbl:"
./dbl $FILE
exit 0
Which produces:
% ./s1
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
cat (coreutils) 5.2.1
grep (GNU grep) 2.5.1
Input file data1:
First line.$
Next line has no characters.$
$
Next line has 1 space.$
$
Next line has 1 TAB.$
^I$
Penultimate line.$
Last line.$
Processed with grep '.':
First line.
Next line has no characters.
Next line has 1 space.
Next line has 1 TAB.
Penultimate line.
Last line.
Processing with dbl:
First line.
Next line has no characters.
Next line has 1 space.
Next line has 1 TAB.
Penultimate line.
Last line.