Search code examples
textterminalgrepnewline

Remove blank lines with grep


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


Solution

  • 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