带有空数据的 --line-regexp 选项 [英] --line-regexp option with null data
问题描述
考虑这个命令:
printf 'alpha
bravo
charlie
' | grep --line-regexp --quiet bravo
grep 看到由换行符分隔的 3 行,并匹配 bravo 行.现在考虑这个命令:
grep sees 3 lines separated by newline, and matches the bravo line. Now consider this command:
printf 'alpha bravo charlie ' | grep --line-regexp --quiet bravo
我的想法告诉我,因为我没有使用过--null-data
,所以grep应该会看到1 甚至 0 行由换行符分隔,并且无法匹配 bravo
后跟新队.但是它没有,它就像第一个命令一样成功,为什么这是?
My thinking tells me that because I have not used --null-data
, grep should see
1 or even 0 lines separated by newline, and fail to match a bravo
followed by
newline. However it does not, it succeeds just like the first command, why is
this?
推荐答案
This behavior was introduced with Grep 2.21:
搜索二进制数据时,grep 现在可以将非文本字节视为行终结者.这可以显着提高性能.
When searching binary data, grep now may treat non-text bytes as line terminators. This can boost performance significantly.
那么现在发生的事情是对于二进制数据,所有非文本字节(包括换行符)被视为行终止符.如果你想改变这个行为,您可以:
So what happens now is that with binary data, all non-text bytes (including newlines) are treated as line terminators. If you want to change this behavior, you can:
使用
--text
.这将确保只有换行符是行终止符
use
--text
. This will ensure that only newlines are line terminators
使用--null-data
.这将确保只有空字节是行终止符
use --null-data
. This will ensure that only null bytes are line terminators
这篇关于带有空数据的 --line-regexp 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!