title: "grep" offset of ascii string from binary file
body: <p>I'm generating binary data files that are simply a series of records concatenated together.  Each record consists of a (binary) header followed by binary data.  Within the binary header is an ascii string 80 characters long.  Somewhere along the way, my process of writing the files got a little messed up and I'm trying to debug this problem by inspecting how long each record actually is.</p>
  
  <p><a href="https://stackoverflow.com/questions/4180081/linux-binary-grep">This</a> seems extremely related, but I don't understand perl, so I haven't been able to get the accepted answer there to work.  The other answer points to <code>bgrep</code> which I've compiled, but it wants me to feed it a hex string and I'd rather just have a tool where I can give it the ascii string and it will find it in the binary data, print the string and the byte offset where it was found.</p>
  
  <p>In other words, I'm looking for some tool which acts like this:</p>
  
  <pre><code>tool foobar filename
  </code></pre>
  
  <p>or</p>
  
  <pre><code>tool foobar &lt; filename
  </code></pre>
  
  <p>and its output is something like this:</p>
  
  <pre><code>foobar:10
  foobar:410
  foobar:810
  foobar:1210
  ...
  </code></pre>
  
  <p>e.g. the string which matched and a byte offset in the file where the match started.  In this example case, I can infer that each record is 400 bytes long.</p>
  
  <p>Other constraints:</p>
  
  <ul>
  <li>ability to search by regex is cool, but I don't need it for this problem</li>
  <li>My binary files are big (3.5Gb), so I'd like to avoid reading the whole file into memory if possible.</li>
  </ul>