Search code examples
rubygrepbinary-databinutils

Ruby grep binary files?


I have been using this so far

system 'strings binary-file.dmp | grep search_string'

Is there something more "Ruby like"?


Solution

  • Here is my quick-and-dirty strings.rb:

    #! /usr/bin/ruby
    
    ARGV.each do |filename|
        File.open filename,"rb" do |f|
            f.each_line do |line|
                line.scan(/([\x32-\x7e]{4,})/).each {|a| puts a[0]}
            end
        end
    end
    

    This is not a work of art or anything - it only works in straight US-ASCII. The regex bascially means "all groups of printable US-ASCII characters of size 4 charaters or more". This is essentially what strings(1) does.