有时候,我们要搜索二进制文件,比如kernel panic的时候,要搜索哪个内核模块包含特定的指令:
Code: 00041940 02031821 8cab0008 <8c660010> 8c6c0014 8cad000c 00cb5821 018d6021 0166302b
此时需要二进制搜索功能。
$ LANG=C grep --only-matching --byte-offset --binary --text --perl-regexp "\x12\xaf" <file>
缩写:
$ LANG=C grep -obUaP "<\x-hex pattern>" <file>
例子:
LANG=C grep -obUaP "\x01\x02" /bin/grep
参考:
https://syntaxfix.com/question/35414/using-grep-to-search-for-hex-strings-in-a-file