mgi*_*son 27 linux binary bash grep ascii
我正在生成二进制数据文件,这些文件只是连接在一起的一系列记录.每条记录包含一个(二进制)标题,后跟二进制数据.在二进制头内是一个长度为80个字符的ascii字符串.在某个地方,我编写文件的过程有点搞砸了,我试图通过检查每条记录的实际长度来调试这个问题.
这似乎非常相关,但我不理解perl,所以我无法在那里得到公认的答案.bgrep我编译的其他答案指向,但它希望我提供一个十六进制字符串,我宁愿只有一个工具,我可以给它ascii字符串,它会在二进制数据中找到它,打印字符串和找到它的字节偏移量.
换句话说,我正在寻找一些像这样的工具:
tool foobar filename
Run Code Online (Sandbox Code Playgroud)
要么
tool foobar < filename
Run Code Online (Sandbox Code Playgroud)
它的输出是这样的:
foobar:10
foobar:410
foobar:810
foobar:1210
...
Run Code Online (Sandbox Code Playgroud)
例如匹配的字符串和匹配开始的文件中的字节偏移量.在这个示例中,我可以推断每条记录的长度为400字节.
其他限制:
Har*_*non 32
grep --byte-offset --only-matching --text foobar filename
Run Code Online (Sandbox Code Playgroud)
该--byte-offset选项打印每个匹配行的偏移量.
该--only-matching选项使其为每个匹配实例而不是每个匹配行打印偏移量.
该--text选项使grep将二进制文件视为文本文件.
您可以将其缩短为:
grep -oba foobar filename
Run Code Online (Sandbox Code Playgroud)
它适用于GNU版本grep,默认情况下附带linux.它不适用于BSD grep(默认情况下附带Mac).
Tho*_*hor 27
你可以使用strings这个:
strings -a -t x filename | grep foobar
Run Code Online (Sandbox Code Playgroud)
用GNU binutils测试.
例如,/bin/ls确实--help发生的地方:
strings -a -t x /bin/ls | grep -- --help
Run Code Online (Sandbox Code Playgroud)
输出:
14938 Try `%s --help' for more information.
162f0 --help display this help and exit
Run Code Online (Sandbox Code Playgroud)