Law*_*nce 5 linux command-line binary files byte
示例:我有文件“mybinaryfile”,十六进制内容为:
A0 01 00 FF 77 01 77 01 A0
Run Code Online (Sandbox Code Playgroud)
我需要知道这个文件中有多少个 A0 字节,多少个 01,等等。结果可能是:
A0: 2
01: 3
00: 1
FF: 1
77: 2
Run Code Online (Sandbox Code Playgroud)
有什么方法可以直接在 shell 中进行计数,还是我需要用任何语言编写程序来完成这个特定任务?
Ste*_*itt 19
这使用od显示每行一个十六进制值,然后排序和计数:
od -t x1 -w1 -v -An mybinaryfile | sort | uniq -c
Run Code Online (Sandbox Code Playgroud)
(-w1是一个扩展,它不是POSIX强制要求的。)
使用 Perl 将 slurped 文件解压为字节数组,然后使用哈希计算唯一字节数:
printf '\xA0\x01\x00\xFF\x77\x01\x77\x01\xA0' |
perl -0777 -nE '
@bytes = unpack("C*",$_)
}{
$counts{$_}++ for @bytes;
for $k (sort { $a <=> $b } keys %counts) {
printf "%02X: %d\n", $k, $counts{$k}
}
'
00: 1
01: 3
77: 2
A0: 2
FF: 1
Run Code Online (Sandbox Code Playgroud)
如果有足够新的版本List::MoreUtils可用,您可以使用其frequency功能简化计数。