I have a binary file type (many instances of these files) where I would like to replace the string "DC-GX850" with "DMC-GX95". Fortunately the two strings are of equal length. The files may be as large as 22MB. Typically the string occurs twice in the file, within the first 1000 bytes. both need to be replaced.
My operating system is MacOS 10.14.6, running standard BASH.
I have two parts of the solution working, but I am unable to combine them. For my test case, the source file is P1000047.RW2, and the modified file is P1000047D.RW2; "44434d2d47583835" is the hex representation of "DMC-GX95"; 3a4 and 10f0 are the file offsets for the start of the strings for this instance of the file; other files may have different offsets.
This code correctly patches the file:
echo "3a4: 44434d2d47583835" | xxd -r - P1000047D.RW2
echo "10f0: 44434d2d47583835" | xxd -r - P1000047D.RW2
This code produces the two strings needed for the patch:
strings -t x P1000047.RW2 | grep "DC-GX850" | sed s/\ DC-GX850/:\ 44434d2d47583835/
Result:
3a4: 44434d2d47583835
10f0: 44434d2d47583835
However, when I try to combine the two, this code only executes the first patch
strings -t x P1000047.RW2 | grep "DC-GX850" | sed s/\ DC-GX850/:\ 44434d2d47583835/ | xxd -r - P1000047D.RW2
I've tried using sed with the -n option but still unable to get it to work.
I could of course write the script as a loop, but I'd like to avoid that if possible. Any suggestions?