Sample input file:
abcd <Space> 1234 <Space>
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Pass it through
sed command deletes all lines that are empty or that only contains spaces and/or tabs. The regular expression
^[[:blank:]]*$ matches the empty/blank lines, and the trailing
d is a
sed editing command that deletes them.
sed '/^[[:blank:]]*$/d' <file >newfile
newfile will be the edited variant of
file with the empty lines removed.
Alternatively, only output the lines with non-blank characters on them:
sed -n '/[^[:blank:]]/p' <file >newfile
This prints each line that contains at least one character that is not space or tab. The rest of the lines are ignored. The
-n option to
sed disables the default output of every line, and the trailing
p is the
sed command that outputs the current line (if it matches the preceding regular expression).
if you want to get rid of lines containing only spaces, as well as empty lines:
grep '[^ ]' # ie, match any lines with at least 1 char different from space
grep -v '^ *$' # ie, hide lines that only contain spaces (0 or n spaces, ie empty lines as well)
if you want to also get rid of empty lines or lines with just "tabs or spaces"
grep -v "^[ $(printf '\t')]*$"