二进制 sed 替换

发布于 2024-08-28 05:02:00 字数 513 浏览 7 评论 0原文

我试图在二进制文件中进行 sed 替换,但我开始相信这是不可能的。本质上我想做的类似于以下内容:

sed -bi "s/\(\xFF\xD8[[:xdigit:]]\{1,\}\xFF\xD9\)/\1/" file.jpg

我希望实现的逻辑是:扫描二进制文件直到十六进制代码 FFD8,继续读取直到 FFD9,并只保存它们之间的内容(丢弃前后的垃圾,但包括 FFD8FFD9 作为文件的保存部分)

有没有好的方法可以做到这一点?即使不使用 sed?

编辑:我只是在玩弄,找到了最干净的方法(IMO)。我知道这个 grep 语句会表现得很贪婪。

hexdump -ve '1/1 "%.2x"' dirty.jpg | grep -o "ffd8.*ffd9" | xxd -r -p > clean.jpg

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/\(\xFF\xD8[[:xdigit:]]\{1,\}\xFF\xD9\)/\1/" file.jpg

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)

Is there a good way to do this? Even if not using sed?

EDIT: 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

北城挽邺 2024-09-04 05:02:00

bbe 是“用于二进制文件的 sed”,对于大型二进制文件应该比十六进制转储更有效/重建。

其使用示例:

$ bbe -e 's/original/replaced/' infile > outfile

有关手册页的更多信息。

bbe is a "sed for binary files", and should work more efficiently for large binary files than hexdumping/reconstructing.

An example of its use:

$ bbe -e 's/original/replaced/' infile > outfile

Further information on the man page.

酒解孤独 2024-09-04 05:02:00

老问题,但是,

xxd infile | sed 's/xxxx xxxx/yyyy yyyy/' | xxd -r > outfile

可能是最简单、最可靠的解决方案。与OP中的编辑类似。

Old question, but,

xxd infile | sed 's/xxxx xxxx/yyyy yyyy/' | xxd -r > outfile

is probably the simplest and most reliable solution. Similar to the edit in the OP.

坏尐絯℡ 2024-09-04 05:02:00

sed 或许可以做到这一点,但可能会很棘手。这是一个执行相同操作的 Python 脚本(请注意,它会就地编辑文件,我假设您希望根据 sed 脚本执行此操作):

import re

f = open('file.jpeg', 'rb+')
data = f.read()
match = re.search('(\xff\xd8[0-9A-fa-f]+)\xff\xd9', data)
if match:
    result = match.group(1)
    f.seek(0)
    f.write(result)
    f.truncate()
else:
    print 'No match'
f.close()

sed might be able to do it, but it could be tricky. Here's a Python script that does the same thing (note that it edits the file in-place, which is what I assume you want to do based on your sed script):

import re

f = open('file.jpeg', 'rb+')
data = f.read()
match = re.search('(\xff\xd8[0-9A-fa-f]+)\xff\xd9', data)
if match:
    result = match.group(1)
    f.seek(0)
    f.write(result)
    f.truncate()
else:
    print 'No match'
f.close()
早茶月光 2024-09-04 05:02:00

有没有好的方法来做到这一点

的,当然可以使用图像编辑工具,例如 ImageMagick 中的图像编辑工具(在网上搜索 linux jpeg 、 exif 编辑器等),它知道如何编辑 jpg 元数据。我相信您一定能找到一款适合您的工具。不要试图以困难的方式做到这一点。 :)

Is there a good way to do this

yes of course, use an image editing tool such as those from ImageMagick (search the net for linux jpeg , exif editor etc) that knows how to edit jpg metadata. I am sure you can find one tool that suits you. Don't try to do this the hard way. :)

巨坚强 2024-09-04 05:02:00

另外,如果未安装 Python,此 Perl 可能会工作(未经测试,买者自负)...

open(FILE, "file.jpg") || die "no open $!\n";
while (read(FILE, $buff, 8 * 2**10)) {
    $content .= $buff;
}
@matches = ($content =~ /(\xFF\xD8[:xdigit:]+?\xFF\xD9)/g;
print STDOUT join("", @matches);

您需要添加 binmode(FILE); binmode(STDOUT);(在 DOS 或 VMS 上,在 open() 调用之后) - 在 Unix 上不需要。

Also, this Perl might work (not tested, caveat emptor)... if Python is not installed :)

open(FILE, "file.jpg") || die "no open $!\n";
while (read(FILE, $buff, 8 * 2**10)) {
    $content .= $buff;
}
@matches = ($content =~ /(\xFF\xD8[:xdigit:]+?\xFF\xD9)/g;
print STDOUT join("", @matches);

You need to add binmode(FILE); binmode(STDOUT); on DOS or VMS after the open() call - not needed on Unix.

迷路的信 2024-09-04 05:02:00
sed -i "s/$(python -c "print('\x1f', end='')")/;/g" file
sed -i "s/$(python -c "print('\x1f', end='')")/;/g" file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文