views:

1062

answers:

3

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?

+1  A: 

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 using dd for extracting specific bytes and then convert them.

Am
+2  A: 

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}')
R Samuel Klatchko
works great. thanks. I actually found a better way to get the date from the files than parsing the binary data before I read this. But this code does do what it's supposed to. Thanks!
Joel
A: 

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

ghostdog74