使用 sed 替换十六进制字符串 [英] Hex String Replacement Using sed

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

问题描述

我在使用 sed 查找/替换某些十六进制字符时遇到了一些麻烦.我想替换以下十六进制字符串文件中的所有实例:

I'm having some trouble getting sed to do a find/replace of some hex characters. I want to replace all instances within a file of the following hexadecimal string:

0x0D4D5348

带有以下十六进制字符串:

with the following hexadecimal string:

0x0D0A4D5348

我该怎么做?

我正在尝试进行十六进制查找/替换.输入文件中没有0x0D4D5348"的字面值,但确实有该值的 ASCII 表示.

I'm trying to do a hex find/replace. The input file does not have the literal value of "0x0D4D5348" in it, but it does have the ASCII representation of that in it.

推荐答案

GNU sed v3.02.80、GNU sed v1.03 和 HHsed v1.5 by Howard Helman都支持符号\xNN,其中NN"是两个有效的十六进制数,00-FF.

GNU sed v3.02.80, GNU sed v1.03, and HHsed v1.5 by Howard Helman all support the notation \xNN, where "NN" are two valid hex numbers, 00-FF.

以下是替换二进制文件中的十六进制序列的方法:

Here is how to replace a HEX sequence in your binary file:

$ sed 's/\x0D\x4D\x53\x48/\x0D\x0A\x4D\x53\x48/g' file > temp; rm file; mv temp file

正如@sputnik 所指出的,您可以使用 sed 的 in place 功能.不过有一点需要注意,如果您在 OS/X 上使用它,则必须添加一组空引号:

As @sputnik pointed out, you can use sed's in place functionality. One caveat though, if you use it on OS/X, you'd have to add an empty set of quotes:

$ sed '' 's/\x0D\x4D\x53\x48/\x0D\x0A\x4D\x53\x48/g' file

作为 sed in place 在 OS/X 接受一个参数来指示备份时要添加到文件名的扩展名,因为它确实em> 首先创建一个临时文件.但是然后.. OS/X 的 sed 不支持 \x.

As sed in place on OS/X takes a parameter to indicate what extension to add to the file name when making a backup, since it does create a temp file first. But then.. OS/X's sed doesn't support \x.

这篇关于使用 sed 替换十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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