Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash: Split binary file at predefined positions

Tags:

bash

split

binary

I have binary files which contain data structures of various length. I would like to save these blocks of data into separate files. The size of each block is known. The split command can, well, split a file but it does not stop after the first block of data. It slices the file into pieces of equal size.

Therefore, my current solution is to split and cat the remainder of the file back together, iterating my way through the data. This is very clumsy and may even fail in certain circumstances.

What is the best way to slice a binary file precisely at certain positions?

like image 797
Aziraphale Avatar asked Mar 04 '15 14:03

Aziraphale


1 Answers

You can use two independent dd commands. One to seek arbitrarily, and another to copy arbitrary lengths.

SEEK=501
BYTES=387
dd if=yourfile bs=$SEEK skip=1 | dd bs=$BYTES count=1 > lump.bin

Note: Although counter-intuitive to what you are actually try to do, keep the blocksize high and the count low for best performance. What I mean is, if you want 8192 bytes, use bs=8192 count=1 rather than bs=1 count=8192.

like image 124
Mark Setchell Avatar answered Sep 26 '22 01:09

Mark Setchell