I have binary files that should be text (they're exported logs), but I can't open it with less (it looks ugly - it looks like a binary file). I found that I could open it with vi and I can cat it (you'll see the actual logs), but what I'd really like to do is grep through them (without having to open up each one with vi and then perform a search). Is there a way for me to do that?
You can use
grep
anyway to search through the file - it does not really care if the input file is really text or not. From 'man grep':Please mark the words of caution at the end of the second paragraph. You might want to redirect the results from grep into a new file and examine this with vi / less.
Pipe it through
strings
, which will strip out all of the binary code leaving just the text.Give
bgrep
a try. (original release / more recent fork)You can use these three commands:
grep -a <sth> file.txt
cat -v file.txt | grep <sth>
cat file.txt | tr '[\000-\011\013-\037\177-\377]' '.' | grep <sth>
Starting with Grep 2.21, binary files are treated differently:
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 terminatorsuse
--null-data
. This will ensure that only null bytes are line terminators