Why does grep report "Binary file matches"?

Today I found an interesting grep alert when trying to find whether my cron job is running or not.

[root@server ]$ cat /var/log/cron | grep  something.php
Binary file (standard input) matches
[root@server ]$

I've done something like this for as long as I know, none of above alert ever comes up.

So, why grep comes up with those alert?
Here is a clue from grep manual:

Quote:
If the first few bytes of a file indicate that the file contains binary data, assume that the file is of type type. By default, type is ‘binary’, and grep normally outputs either a one-line message saying that a binary file matches, or no message if there is no match. If type is ‘without-match’, grep assumes that a binary file does not match; this is equivalent to the ‘-I’ option. If type is ‘text’, grep processes a binary file as if it were text; this is equivalent to the ‘-a’ option. Warning: ‘--binary-files=text’ might output binary garbage, which can have nasty side effects if the output is a terminal and if the terminal driver interprets some of it as commands.

As you reckon, we need to add -a option.

[root@server ]$ cat /var/log/cron | grep -a something.php
....
Apr 27 00:00:01 server crond[9475]: (root) CMD (cd /var/www/html/ ; /usr/bin/php something.php > /dev/null 2>&1)
Apr 28 00:00:01 server crond[29336]: (root) CMD (cd /var/www/html/ ; /usr/bin/php something.php > /dev/null 2>&1)
Apr 29 00:00:01 server crond[23088]: (root) CMD (cd /var/www/html/ ; /usr/bin/php something.php > /dev/null 2>&1)
[root@server ]$

Comments

Excellent thank you very much, I served me well. :)

2011-xxxxxx8,068 ERROR [NSIProcessor] For input string: "
2011-xxxxxx ERROR [NSIProcessor] For input string: "�"

Thanks for the article ... I got your tips within 5 minutes ... when I was facing the same message

Can you pipe the error to a file?

$ grep -r -o "pattern" >> output file
Binary file foo1 matches
Binary file foo2 matches

I want "Binary file foo1 matches", etc. to be redirected into output file.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

If you enjoyed this post, make sure you subscribe to our RSS Feed! Or if you prefer, you can Follow us on Twitter instead.

This is my notes page -- a scratch pad. Sometimes I do not write them correctly. I wrote here so I can access my notes from everywhere. If you want to use them, use with caution. Please check and re-check the syntax. You've been warned!