I have a binary file called rockx.dat, and a bunch of other binary files rockx_#.pmf.
I want to find the contents of the pmf files in the dat file, and replace them with FF. So if the pmf file is 500 bytes, I want to replace it with 500 FF bytes.
I have a binary file called rockx.dat, and a bunch of other binary files rockx_#.pmf.
I want to find the contents of the pmf files in the dat file, and replace them with FF. So if the pmf file is 500 bytes, I want to replace it with 500 FF bytes.
You could use xxd
for your application.
In order to process the binary file you would need multiple steps:
#!/bin/bash
file_orig="rockx.dat"
file_subst="rockx_0.pmf"
# could use tmpfile here
tmp_ascii_orig="rockx.ascii"
tmp_ascii_subst="subst.ascii"
# convert files to ascii for further processing
xxd -p "${file_orig}" "${tmp_ascii_orig}"
xxd -p "${file_subst}" "${tmp_ascii_subst}"
# remove newlines in converted files to ease processing
sed -i ':a;N;$!ba;s/\n//g' "${tmp_ascii_orig}"
sed -i ':a;N;$!ba;s/\n//g' "${tmp_ascii_subst}"
# create a 0xff pattern file for pattern substitution
ones_file="ones.ascii"
dd if=<(yes ff | tr -d "\n") of="${ones_file}" count="$(($(stat -c %s "${tmp_ascii_subst}") - 1))" bs=1
# substitute the pattern in the original file
sed -i "s/$(cat "${tmp_ascii_subst}")/$(cat "${ones_file}")/" "${tmp_ascii_orig}"
# split the lines again to allow conversion back to binary
sed -i 's/.\{60\}/&\n/g' "${tmp_ascii_orig}"
# convert back
xxd -p -r "${tmp_ascii_orig}" "${file_orig}"
For more information on the newline substitution have a look here.
For more information regarding the pattern file creation have a look here.
For information about the line splitting have a look here.
And for information about xxd
hve a look in the manpage.
Please note that this is only for one pattern substitution but it should be possible to change this to serve multiple substitutions with multiple files without high effort.
dd: failed to open '<(yes ff | tr -d \n)': No such file or directory
./substitute.sh: line 23: /usr/bin/sed: Argument list too long
line 23 being
– Mony Armenchev Apr 09 '20 at 14:24sed -i "s/$(cat "${tmp_ascii_subst}")/$(cat "${ones_file}")/" "${tmp_ascii_orig}"