Remove blank lines with grep

后端 未结 15 1944
后悔当初
后悔当初 2020-12-22 17:51

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

15条回答
  •  时光说笑
    2020-12-22 18:12

    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
    

提交回复
热议问题