- 具有空数据的--line-regexp选项 [英] --line-regexp option with null data
问题描述
printf'alpha\\\
bravo\\\
charlie\\\
'| grep --line-regexp --quiet bravo
grep看到由换行符分隔的3行,布拉沃线。现在考虑
这个命令:
printf'alpha\0bravo\0charlie\0'| grep --line-regexp --quiet bravo
我的想法告诉我,因为我没有使用 --null-data
,grep应该看到
1甚至0行被换行符隔开,并且无法匹配 bravo
后跟
换行符。然而它并没有,它就像第一个命令一样成功,为什么是
这个?
这种行为与Grep 2.21一起推出:
在搜索二进制数据时,grep现在可以将非文本字节视为
行结束符。这可以显着提高性能。
现在发生的情况是,对于二进制数据,所有非文本字节 这将确保只有空字节是行终止符
(包括换行符)被视为行终止符。如果您想更改此
行为,您可以:使用 -
。这将确保只有换行符是行终止符。使用
< - null-data
。
Consider this command:
printf 'alpha\nbravo\ncharlie\n' | grep --line-regexp --quiet bravo
grep sees 3 lines separated by newline, and matches the bravo line. Now consider this command:
printf 'alpha\0bravo\0charlie\0' | grep --line-regexp --quiet 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:
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:
use
--text
. This will ensure that only newlines are line terminatorsuse
--null-data
. This will ensure that only null bytes are line terminators
这篇关于 - 具有空数据的--line-regexp选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!