How to extract specific bytes from a file using unix

how to extract 12-byte fragments from a binary file at specific file positions.

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 an easier way if I have something like 20 12 byte chunks that I need to extract

thanks

+6
file extract byte
source share
2 answers

Use dd :

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

You can write a shell loop to substitute numbers.

You can also consider using awk , Perl, or Python if there are many, or it should be very fast.

+15
source share

Using xxd:

 xxd -p -seek 3d -l 12 file.bin > output 

3d means 61 in hex

Using hexdump:

 hexdump -ve '16/1 "%0.2x " "\n"' -s 3d -n 12 file.bin > output 
+1
source share

All Articles