如何通过看起来像文本的二进制文件进行 grep?

Rob*_*ith 76 text grep parsing binary

我有应该是文本的二进制文件(它们是导出的日志),但我无法用 less 打开它(它看起来很丑 - 它看起来像一个二进制文件)。我发现我可以用 vi 打开它,我可以 cat 它(你会看到实际的日志),但我真正想做的是通过它们 grep(不必用 vi 打开每一个然后执行搜索)。有没有办法让我做到这一点?

Axe*_*auf 85

grep无论如何都可以使用来搜索文件 - 它并不真正关心输入文件是否真的是文本。来自“男人 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.
Run Code Online (Sandbox Code Playgroud)

请在第二段末尾标出警告语。您可能希望将 grep 中的结果重定向到一个新文件中,并使用 vi/less 进行检查。


Mik*_*ott 41

通过管道strings将其删除,这将删除所有二进制代码,只留下文本。


qua*_*nta 6

bgrep一试。(原始版本/最近的分支


小智 5

您可以使用这三个命令:

  1. grep -a <sth> file.txt

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

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