使用 sed 替换十六进制字符串

发布于 2024-12-09 18:19:06 字数 243 浏览 4 评论 0原文

我在让 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 技术交流群。

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

发布评论

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

评论(3

梦途 2024-12-16 18:19:06

GNU sed v3.02.80、GNU sed v1.03 和 HHsed v1.5(作者:Howard Helman)
全部支持符号 \xNN,其中“NN”是两个有效的十六进制数字,00-FF。

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

$ 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 上使用它,则必须添加一组空引号:

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

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:

$ sed 's/\x0D\x4D\x53\x48/\x0D\x0A\x4D\x53\x48/g' file > temp; rm file; mv temp 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:

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

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.

用心笑 2024-12-16 18:19:06

这对我在 Linux 和 OSX 上有效。

就地替换:(

sed -i '.bk' 's'/`printf "\x03"`'/foo/g' index.html

参见@tolitius的答案中@Ernest的评论)

This worked for me on Linux and OSX.

Replacing in-place:

sed -i '.bk' 's'/`printf "\x03"`'/foo/g' index.html

(See @Ernest's comment in the answer by @tolitius)

缪败 2024-12-16 18:19:06

在 OS/X 系统的 Bash 中,你可以使用这样的命令:

# this command will crate a variable named a which contains '\r\n' in it
a=`echo -e "hello\r\nworld\r\nthe third line\r\n"`

echo "$a" | sed 

现在你可以看到输出字符:

0000000    h   e   l   l   o  \n   w   o   r   l   d  \n   t   h   e
0000020    t   h   i   r   d       l   i   n   e  \n
0000033

你应该注意到 's/\r//g'$ 之间的区别's/\r//g'
根据上面的做法,可以使用这样的命令来替换hex String

echo "$a" | sed 
s/\r//g' | od -c

现在你可以看到输出字符:


你应该注意到 's/\r//g'$ 之间的区别's/\r//g'
根据上面的做法,可以使用这样的命令来替换hex String


s/\x0d//g'  | od -c
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:

# this command will crate a variable named a which contains '\r\n' in it
a=`echo -e "hello\r\nworld\r\nthe third line\r\n"`

echo "$a" | sed 

and now you can see the output characters :

0000000    h   e   l   l   o  \n   w   o   r   l   d  \n   t   h   e
0000020    t   h   i   r   d       l   i   n   e  \n
0000033

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

echo "$a" | sed 
s/\r//g' | od -c

and 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


s/\x0d//g'  | od -c
s/\r//g' | od -c

and 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文