简体   繁体   English

用'grep'匹配文件中的空行

[英]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?当文件中有空行时返回 0

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 并像这样计算空行:

grep -c "^$" myfile.txt

As \\n is considered end of line you need to use line start and line end "^$"由于\\n被视为行尾,因此您需要使用行开始和行结束"^$"

The other answers with -c are correct. -c的其他答案是正确的。

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 ) (如果你想删除它们: sed -i '/^$/d' myfile.txt

:) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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