二进制 sed 替换 [英] binary sed replacement

查看:67
本文介绍了二进制 sed 替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在二进制文件中进行 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,并且只保存它们之间的内容(丢弃之前和之后的垃圾,但包括 FFD8FFD9 作为文件的保存部分)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆