Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to extract specific bytes from a file using unix

Tags:

file

byte

extract

how do I extract 12byte chunks from a binary file at certain positions within the file.

If I wanted to extract the first 12 bytes I could do something like

head -c12 file.bin>output

If I wanted to extract 12 bytes from byte61 I could do something like

head -c72 file.bin|tail -c12 >output

Is there a simpler way if I have something like 20 12byte chunks I need to extract

thanks

like image 947
monkeyking Avatar asked Jan 07 '10 00:01

monkeyking


1 Answers

Use dd:

dd bs=1 seek=60 count=12 if=file.bin of=output

You can write a shell loop to substitute the numbers.

You could also consider using awk, Perl or Python, if there's a lot of them to do or it needs to be really fast.

like image 121
Andrew McGregor Avatar answered Oct 01 '22 18:10

Andrew McGregor