Remove blank lines with grep

后端 未结 15 1947
后悔当初
后悔当初 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:31

    I tried hard, but this seems to work (assuming \r is biting you here):

    printf "\r" | egrep -xv "[[:space:]]*"
    
    0 讨论(0)
  • 2020-12-22 18:32

    Use:

    grep pattern filename.txt | uniq
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题