It could be any binary file, an image for example like attached. Only want to replace hex strings with another. In this case the string is only one byte 41 or 42 or 4X... with 21 or 22 or 2X using regex and backreference.
Hello bartus11,
Is not working, when I check with hexdump -C , it seems that actually is removing or deleting bytes.
I'd like to replace more than one hex string, in sed is easy only do multiple replacements using -e 's///g' .. -e 's///g' .. -e 's///g'.
Although some versions of sed may work on binary files, the standards only require sed to work on text files. A simple way that should work on most current systems is to use tr:
Some implementations of the tr utility may accept hex ranges, but the standards only specify octal as shown above. If your current locale has an underlying code set that only has single-byte eight-bit characters (such as ASCII or EBCDIC; but not UTF-8), you can skip the LC_ALL=POSIX.
Note, however, that if your image includes geolocation, timestamp, or camera settings data in addition to the actual image, you might not like the results unless you can extract just the image data you want to be processed.
The tr utility translates characters or bytes; not strings or multi-byte sequences, and as I said before, tr isn't required to recognize hex. Look at the man page for tr on your system to determine if your tr utility has additional options or formats that may help you.
I get it, thanks for your explanation and details. I think I'll need to use sed to replace multi-byte sequences and apply "tr " command to the output as workaround of replace with "regex/backreference", but with octal ranges.
It would be nice to know if with some utility or a unique command both task can be reached.