I have a binary file and I would like to remove parts from. By removing I mean deleting those parts and thus making the file's size smaller.

The parts would be between two ASCII strings. So, for example the file would look like this

........ start ABCD end ..... start EFGH end ..... start IJKL end ...........

So in this file, I would like to search for strings "start" and "end" and remove the parts between them.

The way I think I can do it is to

  1. lookup all the locations for "start" and "end"
  2. calculate ranges from that
  3. delete those parts

Now I am using some GUI based Hex editor and I use the "Search All", "Select Range" and "Delete" commands, but I am sure it would be possible to solve it using some powerful command line hex/text editors.

Do you know any solution for this problem which doesn't require using a GUI for looking up, copy & paste on clipboard, select range and delete commands but is just a few lines of command line?

I am interested ini both Linux shell scripts or using some command line hex editors under Windows, or even Python scrips are welcome.

Do you think it is possible to solve this problem just by a simple Regex replace? Are there any regex replace util which handles binary files well?

  • GNU grep has the -a option to process as text, and I think maybe vim could suffice as a hex editor.
    – new123456
    Jun 26 '11 at 18:32

This sounds like a job for perl:

perl -pe 's/start.*?end//g' < inputfile.bin > outputfile.bin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.