如何抑制grep中的二进制文件匹配结果

时间:2022-01-04 03:03:54

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

在linux中使用grep时,结果总是包含许多“二进制文件XXX匹配”,我并不关心这些。如何抑制这部分结果,或者如何排除grep中的二进制文件?

2 个解决方案

#1


148  

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中排除二进制文件。其他是行号和文件名。

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:

这可能是运行grep的一种方式:

grep -InH your-word *

#2


6  

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:

这是一个老问题,已经有人回答过了,但是我想我应该把-二进制文件=文本选项放在这里,供任何想要使用它的人使用。-I选项忽略二进制文件,但如果您希望grep将二进制文件作为文本文件使用——二进制文件=文本,如下所示:

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$

#1


148  

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中排除二进制文件。其他是行号和文件名。

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:

这可能是运行grep的一种方式:

grep -InH your-word *

#2


6  

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:

这是一个老问题,已经有人回答过了,但是我想我应该把-二进制文件=文本选项放在这里,供任何想要使用它的人使用。-I选项忽略二进制文件,但如果您希望grep将二进制文件作为文本文件使用——二进制文件=文本,如下所示:

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$