Binary grep on Linux?

后端 未结 6 1891
你的背包
你的背包 2020-12-13 18:53

Say I have generated the following binary file:

# generate file:
python -c \'import sys;[sys.stdout.write(chr(i)) for i in (0,0,0,0,2,4,6,8,0,1,3,0,5,20)]\'          


        
6条回答
  •  长情又很酷
    2020-12-13 19:25

    This seems to work for me:

    grep --only-matching --byte-offset --binary --text --perl-regexp "<\x-hex pattern>" 
    

    Short form:

    grep -obUaP "<\x-hex pattern>" 
    

    Example:

    grep -obUaP "\x01\x02" /bin/grep
    

    Output (Cygwin binary):

    153: <\x01\x02>
    33210: <\x01\x02>
    53453: <\x01\x02>
    

    So you can grep this again to extract offsets. But don't forget to use binary mode again.

提交回复
热议问题