答案1
如果文件中的任何地方有 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.