用'grep'匹配文件中的空行 [英] Match empty lines in a file with 'grep'
本文介绍了用'grep'匹配文件中的空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么
grep -c '^\n' myfile.txt
文件中有空行时
返回0 ?
如果有一个空行,它以换行开头,对吗?
If there is an empty line, it starts with a new line, right?
如果我错了,请纠正我.
Please correct me if I am wrong.
推荐答案
与行尾匹配的正则表达式为$
,而不是\n
(由于grep
一次工作一行,因此忽略行之间的换行符.
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
这篇关于用'grep'匹配文件中的空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文