Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
serverfault.com/questions/51477/… –  quanta Nov 5 '11 at 16:02
2  
Did you try grep -a? –  quanta Nov 5 '11 at 16:02
add comment

4 Answers 4

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':

    -a, --text
          Process a binary file as if it were text; this is equivalent to the --binary-files=text option.

   --binary-files=TYPE
          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: grep
          --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.

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.

share|improve this answer
add comment

Pipe it through strings, which will strip out all of the binary code leaving just the text.

share|improve this answer
    
Great idea, thanks :) [not OP] –  Karel Bílek Mar 28 '13 at 1:38
add comment

Give bgrep a try.

share|improve this answer
add comment

You can Use three commands I think:

1) grep -a file.txt

2) cat -v file.txt | grep <sth>

3) cat file.txt | tr '[\000-\011\013-\037\177-\377]' '.' | grep <sth>

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.