sed:在二进制文件中查找并替换字符串(使用 $ 和 space 等字符)

sed: find and replace a string (with characters like $ and space) in a binary file

我有一个文件,其中有一个像 tail +390 [=13=] > $outname 这样的字符串。我需要找到这一行并将其替换为 tail -n +390 [=14=] > $outname。我正在尝试这样 sed --

sed -i -e 's/tail +390 $0 > $outname/tail -n +390 $ > $outname' file.bin ;

sed -i -e 's/tail\ +390\ $0\ >\ $outname/tail\ -n\ +390\ $\ > $outname' file.bin ;

我要修改的文件是一个二进制文件。

但我无法让它工作,任何提示将不胜感激。

您只需要在最后添加一个/,因为您的sed 语法不正确。就像 s/search/replace 但它应该是 s/search/replace/optional-modifiers .

sed -i 's/tail +390 $0 > $outname/tail -n +390 $0 > $outname/' file

示例:

$ echo 'tail +390 [=11=] > $outname' | sed 's/tail +390 $0 > $outname/tail -n +390 $0 > $outname/'
tail -n +390 [=11=] > $outname