Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary sed replacement

Tags:

I was attempting to do a sed replacement in a binary file however I am beginning to believe that is not possible. Essentially what I wanted to do was similar to the following:

sed -bi "s/\(\xFF\xD8[[:xdigit:]]\{1,\}\xFF\xD9\)/\1/" file.jpg 

The logic I wish to achieve is: scan through a binary file until the hex code FFD8, continue reading until FFD9, and only save what was between them (discards the junk before and after, but include FFD8 and FFD9 as the saved part of the file)

Is there a good way to do this? Even if not using sed?

EDIT: I just was playing around and found the cleanest way to do it IMO. I am aware that this grep statement will act greedy.

hexdump -ve '1/1 "%.2x"' dirty.jpg | grep -o "ffd8.*ffd9" | xxd -r -p > clean.jpg 
like image 700
Ryan Avatar asked Apr 09 '10 03:04

Ryan


People also ask

Does sed work with binary files?

bbe is a sed-like editor for binary files. Instead of reading input in lines as sed, bbe reads arbitrary blocks from an input stream and performs byte-related transformations on found blocks.

How do you substitute sed?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.

How do I grep a binary file?

To force GNU grep to output lines even from files that appear to be binary, use the -a or ' --binary-files=text ' option. To eliminate the “Binary file matches” messages, use the -I or ' --binary-files=without-match ' option, or the -s or --no-messages option. Why doesn't ' grep -lv ' print non-matching file names?

How do I edit a bin file in Linux?

The xxd command allows us to dump hexadecimal data from a binary file easily. We can also convert hexadecimal data back into a binary file. It's a useful command-line hex editor that's usually part of the vim text editor package. xxd command comes built-in in almost all the major Linux distributions.


1 Answers

bbe is a "sed for binary files", and should work more efficiently for large binary files than hexdumping/reconstructing.

An example of its use:

$ bbe -e 's/original/replaced/' infile > outfile 

Further information on the man page.

like image 109
Ivan Tarasov Avatar answered Nov 05 '22 22:11

Ivan Tarasov