Linux grep двоичный файл совпадает

grep returns «Binary file (standard input) matches» when trying to find a string pattern in file

My bash_history does exist and there are many lines in it that starts with git . What caused it to display this error and how can I fix it?

5 Answers 5

You can use grep -a ‘pattern’ .

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

It has worked for me, but still weird because my file isn’t a binary file. [grid@serverdg2 trace]$ file listener.log listener.log: data

It is enough, if your file has a single non-decodable character, and grep will fail (if not used with -a)

Presumably the file .bash_history starts with non-text data, hence grep is treating the file as binary. This is confirmed by the file .bash_history output:

You can read a few bytes from start to have a conforming view:

Here I am reading first 1 KiB.

You can pipe the STDOUT to hexdump / od or similar.

As a side note, grep takes filename(s) as argument, so cat is useless here; try this:

I’m still not sure how to solve the grep issue, head -c1k .bash_history read the first 38 lines of my .bash_history file. Everything was readable

This can be caused by null bytes in your bash history. Removing lines with null characters may resolve the issue. You can check for them using grep’s Perl-regexp mode:

Читайте также:  Find linux version and bit

This post has suggestions for non-unix systems.

I had the same problem when I want to grep my .bash_history . (Little Note: I renamed my history, so that a new one was created. This new history was not treated as a binary.)

In @heemayls answer it is stated, that grep takes filenames and cat would be useless. This is not entirely true. From grep s man page:

If no files are specified, or if the file “-” is given, grep searches standard input.

So you could use cat and pipe it to grep . However this solves not the problem that .bash_history is treated as a binary. The only right thing is to use grep -a (Like in the answer from @AK_) whether you grep the history directly or with cat and a pipe.

cat .bash_history | grep -a git 

Источник

How to suppress binary file matching results in grep [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

When using grep in linux, the result often contains a lot of «binary file XXX matches», which I do not care about. How to suppress this part of the results, or how to exclude binary files in grep?

The reason for closing this question could have been more clear. It is a proper question for Unix & Linux IMO. Probably, the reason could explain that this question is more suited for another Stack Exchange site.

Читайте также:  Линукс операционная система чем отличается от виндовс

Closing this was silly. grep is definitely among the «software tools primarily used by programmers» Plus, look at all the tags! stackoverflow.com/questions/tagged/grep

2 Answers 2

There are three options, that you can use. -I is to exclude binary files in grep. Other are for line numbers and file names.

grep -I -n -H -I -- process a binary file as if it did not contain matching data; -n -- prefix each line of output with the 1-based line number within its input file -H -- print the file name for each match 

So this might be a way to run grep:

Thank you for clarifying what the short options mean in your answer. There are so many terse linux command answers on SO that give no explanation, which I find annoying.

@AaronFranke: The -n flag tells grep to report the line numbers of files wherein it found a match. «1-based» means that the line counting starts from one rather than zero, as is often done in programming. So, if the first line of your file named example.txt is Hello, world , the second line is Hello cat , and the third line is cats are cool , then searching for «cat» via grep -n cat example.txt , you’d get example.txt:2: Hello cat and example.txt:3: cats are cool .

This is an old question and its been answered but I thought I’d put the —binary-files=text option here for anyone who wants to use it. The -I option ignores the binary file but if you want the grep to treat the binary file as a text file use —binary-files=text like so:

bash$ grep -i reset mediaLog* Binary file mediaLog_dc1.txt matches bash$ grep --binary-files=text -i reset mediaLog* mediaLog_dc1.txt:2016-06-29 15:46:02,470 - Media [uploadChunk ,315] - ERROR - ('Connection aborted.', error(104, 'Connection reset by peer')) mediaLog_dc1.txt:ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer')) bash$ 

Источник

Оцените статью
Adblock
detector