在bash中处理二进制数据文件,查找大于某个数字的元素
我处理不同的二进制数据。大多数情况下,这些都是经过签名的 16 位流。使用 hexdump,它看起来像:
...
2150 -191 -262 15 -344 -883 -820 -1038 -780
-1234 -1406 -693 131 433 396 241 600 1280
...
我只想查看数据流中大于或小于某个阈值的那些元素(数据是二进制有符号的 16 位)。它可能看起来像:
cat data.pcm | $($here_some_filtering) 2100 -2100
其中输出必须只给出大于 2100 且小于 -2100 的元素。有没有简单的命令行方法可以做到这一点?
I process different binary data. Mostly, these are signed 16-bit streams. With hexdump, it looks like:
...
2150 -191 -262 15 -344 -883 -820 -1038 -780
-1234 -1406 -693 131 433 396 241 600 1280
...
I would like to see only those elements of a data stream, which are greater than or less than some threshold (data is binary signed 16-bit). It could look like:
cat data.pcm | $($here_some_filtering) 2100 -2100
where output must give me only elements which are greater than 2100 and less than -2100. Is there any simple command-line method how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对此的一个衬里将是这样的:
One liner for this would be something like:
好吧,二进制...个人建议:不要使用普通的旧 shell - 使用适合该工作的工具。 Perl、Python,甚至 C/C++ 程序 - 其中大部分都是单行代码。
下面是一个未经优化的 hack,可以给你一个想法:
我不确定这是否可以通过简单的方式进行管道传输,因为 shell 使用行分隔符来分割输入块。
Well, binary ... personal suggestion: Do not use plain old shell - use a tool fit for the job. Perl, Python, even a C/C++ program - it'll be mostly one-liners in those.
The following is an unoptimized hack to give you an idea:
I'm not sure this can be made pipe-able in an easy way because of the fact that the shell uses line separators to split input blocks.
Bash 可以处理二进制数据。
示例(数据有意具有奇数个字节,以表明该函数适应此条件):
那么您的命令将是:
Bash can be made to deal with binary data.
Examples (the data has an odd number of bytes intentionally to show that the function accommodates this condition):
Your command would then be:
每当我想从二进制文件中提取数值时,我都会使用 od(八进制转储)。它有许多用于提取字符、整数(8、16、32 和 64 位)和浮点数(32 和 64 位)的选项。您还可以指定您要查找的精确值的偏移量。
要了解更多信息,请输入:
然后,在 bash 中对 od 输出进行过滤应该不会很复杂。
Whenever I want to extract numerical values from a binary file, I use
od
(octal dump). It has many options for extracting characters, integers (8, 16, 32 and 64 bits) and floats (32 and 64 bits). You can also specify an offset to the exact value that you are looking for.For learning more about it, type:
Then, filtering on
od
output should not be complex in bash.