grep returns
Binary file test.log matches
For example
echo \"line1 re \\x00\\r\\nline2\\r\\nline3 re\\r\\n\" > test.log # in zsh
One way is to simply treat binary files as text anyway, with grep --text
but this may well result in binary information being sent to your terminal. That's not really a good idea if you're running a terminal that interprets the output stream (such as VT/DEC or many others).
Alternatively, you can send your file through tr
with the following command:
tr '[\000-\011\013-\037\177-\377]' '.'
This will change anything less than a space character (except newline) and anything greater than 126, into a .
character, leaving only the printables.
If you want every "illegal" character replaced by a different one, you can use something like the following C program, a classic standard input filter:
#include
int main (void) {
int ch;
while ((ch = getchar()) != EOF) {
if ((ch == '\n') || ((ch >= ' ') && (ch <= '~'))) {
putchar (ch);
} else {
printf ("{{%02x}}", ch);
}
}
return 0;
}
This will give you {{NN}}
, where NN
is the hex code for the character. You can simply adjust the printf
for whatever style of output you want.
You can see that program in action here, where it:
pax$ printf 'Hello,\tBob\nGoodbye, Bob\n' | ./filterProg
Hello,{{09}}Bob
Goodbye, Bob