Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Script - Read Binary File

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?

like image 641
Joel Avatar asked Dec 29 '09 00:12

Joel


People also ask

How do I read a binary file?

To open the Binary Editor on an existing file, go to menu File > Open > File, select the file you want to edit, then select the drop arrow next to the Open button, and choose Open With > Binary Editor.


1 Answers

You can use od (plus head and awk for a little post-processing) for this. To get the year:

year=$(od -t x2 --skip-bytes=6 --read-bytes=2 file.moi | head -1 | awk '{print $2}')

For the month:

month=$(od -t x1 --skip-bytes=8 --read-bytes=1 file.moi | head -1 | awk '{print $2}')

And the day:

day=$(od -t x1 --skip-bytes=9 --read-bytes=1 file.moi | head -1 | awk '{print $2}')
like image 168
R Samuel Klatchko Avatar answered Oct 13 '22 07:10

R Samuel Klatchko