Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

the_ron

macrumors newbie
Original poster
Jan 27, 2003
27
3
Charlottesville, VA
I'm attempting to replace the same sequence of hex bytes with a different sequence in multiple files. I've been able to do this for individual files using Hex Fiend, but would like to be able to run a script or command to edit all files of the same type in a folder. I've tried using the sed and tr commands, but they don't seem to want to find/replace multiple hex bytes.

I want to do this to change the header of the RAW files from my Sony A850 camera, which are still unsupported in Aperture and Mac OS. If I change the identifiers in the header, the files will be accepted as A900 RAW files and work fine (the cameras have identical sensors). Ultimately, I need to replace the sequence "38 35 30" with "39 30 30" in 3 locations for each file.
 
Can you translate those to ASCII equivalents and search/replace on those (ie. search for characters instead of hex values)? You might have issues with character encodings or endian-ness though.
 
Try encoding the data from binary to hex with the 'xxd' command, then apply sed, then reverse the output back to binary with xxd again.
http://developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man1/xxd.1.html


How I found this.
Code:
  apropos edit
A long list of editing commands, but nothing stands out as editing binary, and most are oriented to text arranged in lines.

Code:
apropos hex
Aha, some interesting things, including xxd.

The apropos command is your friend.
 
Sorry, didn't read it closely enough, sed won't work unless you have line feeds to end the record.

tr should work though.

cat oldfile | tr '\070\065\060' '\071\060\060' > newfile

Try that first. You can add it to a bash script to do all the files of a certain type.

Be aware that it will replace all the characters of that pattern. If you need to target specific areas of the file, you may want to switch to perl.
 
Sorry, didn't read it closely enough, sed won't work unless you have line feeds to end the record.

tr should work though.

cat oldfile | tr '\070\065\060' '\071\060\060' > newfile

Try that first. You can add it to a bash script to do all the files of a certain type.

Be aware that it will replace all the characters of that pattern. If you need to target specific areas of the file, you may want to switch to perl.

tr doesn't work with patterns. It works with sets. So its two string args are treated as sets, with chars in set1 mapped to chars in set2 by position. As a result, the command given will translate every single character '8'->'9', and every '5' or '0' to '0'. It will also translate every byte having those character codes, even if the interpretation in the image-file is that the byte is binary. This is definitely not what is wanted.

Code:
echo 888 0 0 565 833 | tr '\070\065\060' '\071\060\060' | hexdump -C
00000000  39 39 39 20 30 20 30 20  30 36 30 20 39 33 33 0a  |999 0 0 060 933.|
00000010
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.