在文件Grep中匹配空行 [英] Match empty lines in a file Grep

查看:78
本文介绍了在文件Grep中匹配空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

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

当文件中有空行时返回0
如果有空行,它以一个新行开始吧?

请纠正我,如果我错了。

解决方案

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

  grep -c'^ $'myfile.txt 


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.

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆