二进制 sed 替换 [英] binary sed replacement
问题描述
我试图在二进制文件中进行 sed
替换,但我开始相信这是不可能的.基本上我想做的类似于以下内容:
I was attempting to do a sed
replacement in a binary file however I am beginning to believe that is not possible. Essentially what I wanted to do was similar to the following:
sed -bi "s/(xFFxD8[[:xdigit:]]{1,}xFFxD9)/1/" file.jpg
我希望实现的逻辑是:扫描二进制文件直到十六进制代码FFD8
,继续阅读直到FFD9
,并且只保存它们之间的内容(丢弃之前和之后的垃圾,但包括 FFD8
和 FFD9
作为文件的保存部分)
The logic I wish to achieve is: scan through a binary file until the hex code FFD8
, continue reading until FFD9
, and only save what was between them (discards the junk before and after, but include FFD8
and FFD9
as the saved part of the file)
有什么好办法吗?即使不使用sed
?
Is there a good way to do this? Even if not using sed
?
我只是在玩,找到了最干净的方式来做 IMO.我知道这个 grep 语句会表现得很贪婪.
I just was playing around and found the cleanest way to do it IMO. I am aware that this grep statement will act greedy.
hexdump -ve '1/1 "%.2x"' dirty.jpg | grep -o "ffd8.*ffd9" | xxd -r -p > clean.jpg
推荐答案
bbe is a "sed 用于二进制文件",对于大型二进制文件应该比十六进制转储/重建更有效.
bbe is a "sed for binary files", and should work more efficiently for large binary files than hexdumping/reconstructing.
使用示例:
$ bbe -e 's/original/replaced/' infile > outfile
有关手册页的更多信息.
这篇关于二进制 sed 替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!