あるファイルの内容を別のファイルで見つけてFFに置き換えます。

あるファイルの内容を別のファイルで見つけてFFに置き換えます。

私は rockx.dat というバイナリと rockx_#.pmf という別のバイナリを持っています。

datファイルでpmfファイルの内容を見つけてFFに置き換えたいです。だからpmfファイルが500バイトであれば、500FFバイトに置き換えたいと思います。

ベストアンサー1

xxdアプリケーションで使用できます。
バイナリファイルを処理するにはいくつかの手順が必要です。

#!/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}"

改行の交換の詳細については、以下を確認してください。ここ
スキーマファイルの生成の詳細については、次を参照してください。ここ
行分割の詳細については、次を参照してください。ここ。 hveの
詳細については、xxdマンページを参照してください。

これは単一のパターン置換にのみ機能しますが、多くの労力なしに複数のファイルに対して複数の置換を提供するように変更することが可能である必要があります。

おすすめ記事