我跑
grep -b --byte-offset 'EXTH' Agent_of_Change.mobi
但我只得到“二进制文件 Agent_of_Change.mobi 匹配”。我怎样才能得到字节偏移量呢?
答案1
grep
默认情况下,只会返回火柴对于二进制文件。
您需要告诉grep
将二进制文件视为文本。
grep -a -b 'EXTH' Agent_of_Change.mobi
但是,grep
警告您可能的后果:
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.
答案2
添加-a
以强制打印匹配项,即使它们看起来不可打印。
添加后-o
,它只会打印“EXTH”而不是该行。这也会更改字节偏移量 - 它将打印“EXTH”的偏移量,而不是包含“EXTH”的行开头的偏移量。如果文件实际上不是由线条组成的,这将是一个改进!
或者使用perl -n0777e 'print pos()-length($&),"\n" while /EXTH/g'
对更通用的依赖项替换 GNU grep 依赖项...(警告:将整个文件放入内存中,对于大文件可能会令人不快)