Sep 012015
 

An issue was recently noted where ‘grep‘ was throwing the error:

[email protected] [~]# zgrep "ajax" /usr/local/apache/logs/error_log
Binary file (standard input) matches

The permissions were checked, and several other ‘grep‘ type commands were attempted and all returned the same error. In addition, the ‘head‘, ‘tail‘ and ‘less‘ commands all returned the normal log information to standard output.

In reviewing the issue, if there is a NUL character anywhere in the file, ‘grep‘ (or zgrep, egrep…etc.) will consider it as a binary file.

To solve this, use the ‘-r‘ or – – text flags to process the binary file as if it were standard text; these flags are equivalent to the –binary-files=text option.

egrep -r -a mystring

or

grep -a/--text

The downside to this is that the data returned may well result in binary information being sent to your terminal. This is not a good idea if you’re running a terminal such as VT/DEC or many others that interprets the output stream. Alternatively, you can pipe/output the file through ‘tr’ with the following command:

tr '[\000-\011\013-\037\177-\377]' '.' <test .log | grep whatever

which will change anything less than a space character (except newline) and anything greater than 126, into a . character, leaving only the printable characters.

Nike!

Share This!
 Posted by at 3:35 pm