如何查看具有 application/octet-stream mime 类型的二进制 .log 文件?

如何查看具有 application/octet-stream mime 类型的二进制 .log 文件?

我从服务器下载了一些日志文件,我想在其中搜索特定字符串以进行调试。它们都带有 .log 扩展名。

问题在于一个文件具有plain text document (text/plain)mime 类型,而另一个文件具有Binary (application/octet-stream)mime 类型。

我可以plain text document (text/plain)使用文本编辑器以纯文本形式打开 mime 类型日志文件,但是由于它是二进制文件,因此我不能打开其他文件。

如何查看具有application/octet-streammime 类型的二进制 .log 文件?

在此处输入图片描述

答案1

由此回答是什么让 grep 认为一个文件是二进制的?

如果文件中的任何地方有 NUL 字符,grep 会将其视为二进制文件。

可能有这样的解决方法cat file | tr -d '\000' | yourgrep,首先消除所有空值,然后搜索文件。


我首先尝试使用测试目录中的一个文件:

parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518
parto@subroot:~/Desktop/test$ cat info_pdslpostpaid.log-20160518 | tr -d '\000' > info_pdslpostpaid.log-20160518_edited
parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518  info_pdslpostpaid.log-20160518_edited
parto@subroot:~/Desktop/test$

结果是一个plain text document (text/plain)文本 MIME 文件。

在此处输入图片描述

然后,由于我正在处理多个文件,我尝试对目录中的多个文件运行相同的命令:

parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518  info_pdslpostpaid.log-20160520  info_pdslpostpaid.log-20160523  info_pdslpostpaid.log-20160525
parto@subroot:~/Desktop/test$ for i in * ; do cat "$i" | tr -d '\000' > "${i}_edited" ; done
parto@subroot:~/Desktop/test$ ls
info_pdslpostpaid.log-20160518         info_pdslpostpaid.log-20160520_edited  info_pdslpostpaid.log-20160525
info_pdslpostpaid.log-20160518_edited  info_pdslpostpaid.log-20160523         info_pdslpostpaid.log-20160525_edited
info_pdslpostpaid.log-20160520         info_pdslpostpaid.log-20160523_edited
parto@subroot:~/Desktop/test$

太棒了,我所有的日志文件现在都是可读格式的!!:)

答案2

根据您自己的回答,您似乎特别指的是使用搜索文件grep,而不是更改文件的 mime 类型 - 请参阅XY 问题是什么?

如果grep只是根据空字节错误地识别文件,那么您可以使用-a--binary-files=text选项来告诉 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.

相关内容