Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have some database dumps from a Windows system on my box. They are text files. I'm using cygwin to grep through them. These appear to be plain text files; I open them with text editors such as notepad and wordpad and they look legible. However, when I run grep on them, it will say binary file foo.txt matches.

I have noticed that the files contain some ascii NUL characters, which I believe are artifacts from the database dump.

So what makes grep consider these files to be binary? The NUL character? Is there a flag on the filesystem? What do I need to change to get grep to show me the line matches?

share|improve this question
--null-data may be useful if NUL is the delimiter. – Steve-o Sep 1 '11 at 13:27

3 Answers

up vote 4 down vote accepted

If there is a NUL character anywhere in the file, grep will consider it as a binary file.

There might a workaround like this cat file | tr -d '\000' | yourgrep to eliminate all null first, and then to search through file.

share|improve this answer
1  
... or use -a/--text, at least with GNU grep. – derobert Nov 26 '12 at 20:44

You can use the strings utility to extract the text content from any file and then pipe it through grep, like this: strings file | grep pattern.

share|improve this answer

The file /etc/magic or /usr/share/misc/magic has a list of sequences that the command 'file' uses for determining the file type. Note that binary may be just a fallback solution. Sometimes files with strange encoding are considered binary too.

grep on Linux has some options to handle binary files like --binary-files or -U / --binary

share|improve this answer

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.