Remove blank lines with grep

后端 未结 15 2073
后悔当初
后悔当初 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-22 18:32

    It's true that the use of grep -v -e '^$' can work, however it does not remove blank lines that have 1 or more spaces in them. I found the easiest and simplest answer for removing blank lines is the use of awk. The following is a modified a bit from the awk guys above:

    awk 'NF' foo.txt
    

    But since this question is for using grep I'm going to answer the following:

    grep -v '^ *$' foo.txt
    

    Note: the blank space between the ^ and *.

    Or you can use the \s to represent blank space like this:

    grep -v '^\s*$' foo.txt
    

提交回复
热议问题