[英]Count the number of occurences of binary data
我需要在二进制文件中计算十六进制字符串0xFF 0x84 0x03 0x07
的出现次数,没有太多的麻烦...是否有一种从linux命令行中获取此数据的快速方法,或者我应该编写专用代码来执行此操作?
If your version of grep
takes the -P
parameter, then you can use grep -a -P
, to search for an arbitrary binary string inside a binary file. 如果您的
grep
版本采用-P
参数,则可以使用grep -a -P
在二进制文件中搜索任意二进制字符串。 This is close to what you want: 这接近你想要的:
grep -a -c -P '\xFF\x84\x03\x07' myfile.bin
-a
ensures that binary files will not be skipped -a
确保不会跳过二进制文件
-c
outputs the count -c
输出计数
-P
specifies that you pattern is a Perl regular expression, which allows strings to contain hex characters in the above \\xNN
format. -P
指定pattern是Perl正则表达式,它允许字符串包含上述\\xNN
格式的十六进制字符。
Unfortunately, grep -c
will only count the number of "lines" the pattern appears on, even if it appears multiple times on a line. 不幸的是,
grep -c
只计算模式出现的“行”数,即使它在一行上出现多次。 (I'm not sure why that would be a desirable feature). (我不确定为什么这将是一个理想的功能)。
To get the exact number of occurrences with grep
, it seems you need to do: 要使用
grep
获取确切的出现次数,您似乎需要这样做:
grep -a -o -P '\xFF\x84\x03\x07' myfile.bin | wc -l
grep -o
separates out each match onto its own line, and wc -l
counts the lines. grep -o
将每个匹配分离到它自己的行上,并且wc -l
对这些行进行计数。 Note that this depends on the fact that your binary string contains no linebreaks. 请注意,这取决于您的二进制字符串不包含换行符的事实。
use hexdump like 使用hexdump之类的
hexdump -v -e '"0x" 1/1 "%02X" " "' <filename> | grep -oh "0xFF 0x84 0x03 0x07" |wc -w
hexdump
will output binary file in the given format like 0xNN hexdump
将输出给定格式的二进制文件,如0xNN
grep
will find all the occurrences of the string without considering the same ones repeated on a line grep
将查找字符串的所有匹配项,而不考虑在一行上重复的相同字符串
wc
will give you final count wc
会给你最后的计数
did you try grep -a
? 你试过
grep -a
吗?
from grep man page: 来自grep man page:
-a, --text
Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
怎么样:
$ hexdump a.out | grep -Ec 'ff ?84 ?03 ?07'
This doesn't quite answer your question, but does solve the problem when the search string is ASCII but the file is binary: 这不能完全回答你的问题,但是当搜索字符串是ASCII但文件是二进制文件时确实解决了这个问题:
cat binaryfile | sed 's/SearchString/SearchString\n/g' | grep -c SearchString
Basically, 'grep' was almost there except it only counted one occurrence if there was no newline byte in between, so I added the newline bytes. 基本上,'grep'几乎就在那里,除非它只计算一次,如果中间没有换行字节,所以我添加了换行字节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.