Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a binary file and I need to count occurrences of certain string in this file or even list then with offsets. In Windows or Linux- for this task I use Fedora 12 and Windows 2000.

share|improve this question
1  
For Linux: strings -a filename | grep string | wc -l. – ott-- May 15 '12 at 14:38
up vote 7 down vote accepted

On Linux with GNU grep:

grep -F --text -o --byte-offset mystring binaryfile

Example:

$ grep -F --text -o --byte-offset option /bin/tar
226542:option
237529:option
237612:option
...

Explanation of the parameters:

-F, --fixed-strings
      Interpret PATTERN as a list of fixed strings, separated by newlines,
      any of which is to be matched.  (-F is specified by POSIX.)

-a, --text
      Process  a binary file as if it were text; this is equivalent to the
      --binary-files=text option.

-o, --only-matching
      Print  only the matched (non-empty) parts of a matching line, with
      each such part on a separate output line.

-b, --byte-offset
      Print the 0-based byte offset within the input file before each line
      of output. If -o (--only-matching) is specified, print the offset of
      the matching part itself

To count the occurrences add | wc -l to the command line.

share|improve this answer
    
Is it possible to get count only? I know I asked for offsets, but for the count too. – IvanH May 15 '12 at 19:01
1  
@IvanH: Just pipe it to wc -l – ℝaphink May 16 '12 at 5:04
    
Good job including the other parameters! – Simon Sheehan Jul 12 '12 at 21:54

Agent Ransack - Free File Searching Utility

  • Immediate results Found text is shown with highlighted keywords so you don't need to waste time opening each file looking for the
    right information.

  • Boolean expressions Combine search terms using the familiar Boolean operators AND, OR, NOT.

  • Office formats Support for popular Office formats including Office 2007 and OpenOffice.
  • Perl regex Support for Perl compatible regular expressions.
  • 64-bit Version Natively compiled 64-bit version for improved compatibility.
  • Fast searching Highly efficient search algorithms mean that you spend less time waiting for results.
  • Printing and Exporting Results can be shared with others through printing and exporting.

enter image description here

share|improve this answer
    
Can it handle a binary file? – IvanH May 16 '12 at 6:34
    
Yes, it can handle binary values. See the screenshot, it has also found dll files. – user May 16 '12 at 6:42

In Linux, you can use strings -a [filename] | grep [string] | wc -l

In Windows, you can use the strings * | findstr /i [string], with the strings utility from Sysinternals.

share|improve this answer
    
How are multiple occurrences on the same line counted, as one or more? – Cristian Ciupitu May 16 '12 at 13:49

You must log in to answer this question.

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