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).