Remove blank lines with grep

node ninja picture node ninja · Aug 8, 2010 · Viewed 264.2k times · Source

I tried grep -v '^$' in Linux and that didn't work. This file came from a Windows file system.

Answer

ars picture ars · Aug 8, 2010

Try the following:

grep -v -e '^$' foo.txt

The -e option allows regex patterns for matching.

The single quotes around ^$ makes it work for Cshell. Other shells will be happy with either single or double quotes.

UPDATE: This works for me for a file with blank lines or "all white space" (such as windows lines with "\r\n" style line endings), whereas the above only removes files with blank lines and unix style line endings:

grep -v -e '^[[:space:]]*$' foo.txt