Remove empty lines in a text file via grep
grep . FILE
(And if you really want to do it in sed, then: sed -e /^$/d FILE
)
(And if you really want to do it in awk, then: awk /./ FILE
)
Try the following:
grep -v -e '^$'
with awk, just check for number of fields. no need regex
$ more file
hello
world
foo
bar
$ awk 'NF' file
hello
world
foo
bar
Here is a solution that removes all lines that are either blank or contain only space characters:
grep -v '^[[:space:]]*$' foo.txt
If removing empty lines means lines including any spaces, use:
grep '\S' FILE
For example:
$ printf "line1\n\nline2\n \nline3\n\t\nline4\n" > FILE
$ cat -v FILE
line1
line2
line3
line4
$ grep '\S' FILE
line1
line2
line3
line4
$ grep . FILE
line1
line2
line3
line4
See also:
- How to remove empty/blank lines (including spaces) in a file in Unix?
- How to remove blank lines from a file in shell?
- With
sed
: Delete empty lines using sed - With
awk
: Remove blank lines using awk