Я загрузил некоторые файлы журнала с моего сервера, которые я хочу найти для поиска определенной строки в целях отладки. Все они имеют расширение .log.
Проблема в том, что один файл имеет тип пантомимы plain text document (text/plain)
, а другой - тип пантомимы Binary (application/octet-stream)
.
Я могу открыть файл журнала plain text document (text/plain)
MIME-типа в текстовом редакторе как обычный текст, но я не могу, так как он в двоичном виде.
Как просмотреть двоичные файлы .log с типом application/octet-stream
MIME?
От этого ответ в , Что заставляет 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)
текстовый файл пантомимы.
Затем, так как я работаю с несколькими файлами, я пытался выполнить ту же команду для нескольких файлов в каталоге:
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$
И потрясающий, все мои файлы журнала находятся теперь в читаемом формате!! :)
На основе Вашего собственного ответа, Вы, кажется, обращаетесь конкретно к ищущим файлам с помощью grep
, а не к изменению типа пантомимы файла - видят , Какова проблема 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.