0

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?

1
  • 4
    Those succeed and fail appropriately for me. – Etan Reisner Jul 17 '15 at 1:23
2

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 terminators

  • use --null-data. This will ensure that only null bytes are line terminators

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.