使用 sed 替换十六进制字符串
我在让 sed 查找/替换一些十六进制字符时遇到一些麻烦。我想将文件中以下十六进制字符串: 的所有实例替换
0x0D4D5348
为以下十六进制字符串:
0x0D0A4D5348
我该怎么做?
编辑:我正在尝试进行十六进制查找/替换。输入文件中不包含“0x0D4D5348”的字面值,但包含该值的 ASCII 表示形式。
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
How can I do that?
EDIT: 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GNU sed v3.02.80、GNU sed v1.03 和 HHsed v1.5(作者:Howard Helman)
全部支持符号
\xNN
,其中“NN”是两个有效的十六进制数字,00-FF。以下是如何替换二进制文件中的十六进制序列:
正如 @sputnik 指出的,您可以使用 sed 的
in place
功能。但需要注意的是,如果您在 OS/X 上使用它,则必须添加一组空引号:As sed
in place
on OS/X 采用参数表明什么进行备份时添加到文件名的扩展名,因为它首先创建一个临时文件。但后来.. OS/X 的sed
不支持\x
。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:
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: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'ssed
doesn't support\x
.这对我在 Linux 和 OSX 上有效。
就地替换:(
参见@tolitius的答案中@Ernest的评论)
This worked for me on Linux and OSX.
Replacing in-place:
(See @Ernest's comment in the answer by @tolitius)
在 OS/X 系统的 Bash 中,你可以使用这样的命令:
s/\r//g' | od -c现在你可以看到输出字符:
你应该注意到
's/\r//g'
和$ 之间的区别's/\r//g'
。根据上面的做法,可以使用这样的命令来替换hex String
In OS/X system's Bash, You can use command like this:
s/\r//g' | od -cand now you can see the output characters :
You should notice the difference between
's/\r//g'
and$'s/\r//g'
.Based on the above practices, you can use command like this to replace hex String