Bash 脚本 - 读取二进制文件
我是脚本编写新手,但在使用 C# 和 Java 等语言进行编程方面拥有丰富的经验。
我有一个包含二进制数据的文件。我想编写一个 Bash 脚本来读取该文件中包含的年、月和日,以便我可以根据记录日期将关联的 MOD 文件分类到文件夹中。我无法找到读取二进制数据并在 bash 脚本中解析它的方法。有什么办法可以做到这一点吗?
I'm new to scripting, but I have a lot of experience programming in languages such as C# and Java.
I have a file that contains binary data. I want to write a Bash script that reads the year, month, and day contained in that file so I can sort the associated MOD files into folders according to the date they were recorded. I'm having trouble finding a way to read binary data and parsing it in a bash script. Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为此,您可以使用 od (加上 head 和 awk 进行一些后处理)。获取年份: 获取
月份:
获取日期:
You can use od (plus head and awk for a little post-processing) for this. To get the year:
For the month:
And the day:
我建议使用 python 来实现这一点。
但是,如果您坚持使用 bash,我会尝试在二进制模式下使用 sed(从未尝试过)或使用 dd 来提取特定字节,然后进行转换。
I would recommend using python for this.
However, if you insist on bash, i would try using either
sed
in binary mode (never tried it) or usingdd
for extracting specific bytes and then convert them.如果这对您来说不是太难,我建议编译以下 C 语言程序:
然后执行以下操作:
现在您有一个名为“readint”的方便工具,其语法如下:
If this is not too hardcore for you I suggest compiling the following C-language program:
Then do this:
Now you have a handy tool called 'readint' with the following syntax:
您可以在网上搜索解释 MOI 文件的模块(Perl 或 Python)。否则,我真的不认为你可以像从二进制文件中那样获取日期,因为如果你查看内部,它实际上是“垃圾”,因为它是二进制文件。尽管您也可以尝试使用 strings 命令来查看是否有与日期匹配的清晰字符串
you can search the net for modules to interpret MOI files (either Perl or Python). Otherwise, i don't really think you can get the date just like that from the binary file because if you look inside, its really "garbage" since its binary. Although you may also give the strings command a try to see if there are legible strings that match the date