Match empty lines in a file with 'grep'

前端 未结 2 1101
迷失自我
迷失自我 2021-01-01 13:24

Why does

grep -c \'^\\n\' myfile.txt

return 0 when there are empty lines in the file?

If there is an empty line, it starts

相关标签:
2条回答
  • 2021-01-01 13:56

    The regular expression to match the end of the line is $, not \n (since grep works a line at a time, it ignores the newlines between lines).

    grep -c '^$' myfile.txt
    
    0 讨论(0)
  • 2021-01-01 14:16

    grep and count empty lines like this:

    grep -c "^$" myfile.txt
    

    As \n is considered end of line you need to use line start and line end "^$"

    0 讨论(0)
提交回复
热议问题