Trying to get the bytes offset of a text word in a binary file (with some text here and there). I need to know the offset of this word from the beginning of the file. I'm wondering if on osx the command options are slightly different from other unix.

strings Agent_of_Change.mobi | grep -o -b --binary EXTH

This gives me : 31:EXTH

Not sure what 31 is, the bytes from the beginning of the line? The correct value I need would be 1617, from the beginning of the file.

  • My initial thought is that you could save the output of strings to a separate file. My guess is the offset is relating to the output of strings and not to Agents_of_Change.mobi. Also the grep I have, only has --binary-files=XXX and no -binary command - perhaps you have another grep in your path than the OS X provided one? – bmike Apr 3 '14 at 13:39

Grep is likely counting the offset from the output of the strings command since it's not actually parsing the .mobi file directly.

| improve this answer | |
  • I see. The reasons why I'm using strings is that, I don't get the offset when I use this line: "grep -o -b -U EXTH Agent_of_Change.mobi" – aneuryzm Apr 3 '14 at 13:49
  • I just get Binary file Agent_of_Change.mobi matches, but I don't get the offset – aneuryzm Apr 3 '14 at 13:50
  • Yup - grep is protecting you from seeing binary characters and that also is removing the chance for it to tell you the offset of what you already know is a text safe string. You'll likely need a different grep or a different tool to find that offset. – bmike Apr 3 '14 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .