News for hackers. Stuff that matters.
Re:If you use GNU grep on text files, use the -a (--text) op... by arendtio on Tuesday April 21, 2020 @02:52PM (#22938107)
Does someone know if using grep on a binary file is somehow defined by POSIX?

At a glance, I couldn't find a reference on the grep page:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/g...

Re:If you use GNU grep on text files, use the -a (--text) op... by jwilk on Tuesday April 21, 2020 @03:10PM (#22938293)
It isn't. The page says:

> The input files shall be text files.

"Text file" is defined in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1... :

> A file that contains characters organized into zero or more lines. The lines do not contain NUL characters and none can exceed {LINE_MAX} bytes in length, including the <newline> character.

Re:If you use GNU grep on text files, use the -a (--text) op... by arendtio on Tuesday April 21, 2020 @03:22PM (#22938427)
Thank you :-)

Interesting, especially the part about the LINE_MAX. Even though it kinda makes sense, I would never have thought that having a very long line makes a file a non-text file when all characters are 'normal' characters.

Re:If you use GNU grep on text files, use the -a (--text) op... by bawolff on Tuesday April 21, 2020 @08:05PM (#22940647)
Id kind of like that. Grepping in a directory also containing minified js (with ten thousand character long lines) is a pain
Re:If you use GNU grep on text files, use the -a (--text) op... by seppel on Tuesday April 21, 2020 @08:07PM (#22940653)
So, what does trigger grep to regard a file as binary/non-text? For me, it seems these are not only NUL characters.

And I also think that this would not be a discussion if it was just about NUL. There is something else that grep does not like (escape characters? Invalid utf8? Wrong codepage? I don't know), but this makes the binary detection annoying.

Re:If you use GNU grep on text files, use the -a (--text) op... by jwilk on Wednesday April 22, 2020 @04:29AM (#22943405)
I haven't looked at the GNU grep source, but yes, invalid UTF-8 is one of the reasons for treating file as binary:

   $ printf 'a\222b' | LC_ALL=en_US.UTF-8 grep a
   Binary file (standard input) matches
This is POSIX-compliant, because "character" is defined as:

> A sequence of one or more bytes representing a single graphic symbol or control code.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1...