"grep" offset of ascii string from binary file
grep --byte-offset --only-matching --text foobar filename
The --byte-offset
option prints the offset of each matching line.
The --only-matching
option makes it print offset for each matching instance instead of each matching line.
The --text
option makes grep treat the binary file as a text file.
You can shorten it to:
grep -oba foobar filename
It works in the GNU version of grep
, which comes with linux by default. It won't work in BSD grep (which comes with Mac by default).
You could use strings
for this:
strings -a -t x filename | grep foobar
Tested with GNU binutils.
For example, where in /bin/ls
does --help
occur:
strings -a -t x /bin/ls | grep -- --help
Output:
14938 Try `%s --help' for more information.162f0 --help display this help and exit
I wanted to do the same task. Though strings | grep worked, I found gsar was the very tool I needed.
The output looks like:
>gsar.exe -bic -sfoobar filename.binfilename.bin: 0x34b5: AAA foobar BBBfilename.bin: 0x56a0: foobar DDDfilename.bin: 2 matches found