I run
grep -b --byte-offset 'EXTH' Agent_of_Change.mobi
but I only get "Binary file Agent_of_Change.mobi matches". How can I get the bytes offset instead?
grep would, by default, only returns matches for binary files.
You would need to tell grep to treat binary files as text.
grep -a -b 'EXTH' Agent_of_Change.mobi
However, grep warns you of the possible consequences:
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.
strings yourfile | grep searchstring? If that doesn't produce an output, chances are that grep wouldn't either.
grep should as well. Don't use -o (in case you are). grep -a -b searchstring file | cut -d: -f1 should give you the offset that you are looking for.
Add -a to force printing of matches even if they don't seem printable.
Add -o and it'll only print the "EXTH" instead of the line. That also changes the byte offset - it'll print the offset of the "EXTH" instead of the offset of the beginning of the line containing the "EXTH". If the file isn't actually made of lines, this will be an improvement!
Or use perl -n0777e 'print pos()-length($&),"\n" while /EXTH/g' to replace your GNU grep dependency with a dependency on something more universal... (Warning: slurps whole file into memory, may be unpleasant for large files)