简体   繁体   中英

Match empty lines in a file with 'grep'

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 with a new line, right?

Please correct me if I am wrong.

匹配行尾的正则表达式是$ ,而不是\\n (因为grep一次只处理一行,它会忽略行之间的换行符)。

grep -c '^$' myfile.txt

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 "^$"

The other answers with -c are correct.

If you want to see what number is the empty line in the file:

grep -n '^$' myfile.txt

For example:

grep -n '^$' /usr/share/wordlists/rockyou.txt
4751:
34329:
50383:
99361:
154363:
231236:
562997:
2459758:
14344383:
14344384:
14344385:

(If you want to remove them: sed -i '/^$/d' myfile.txt )

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM