Linux sed 替换为十六进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6889093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:22:50  来源:igfitidea点击:

sed replace with hex

linuxsedgrep

提问by toughtalker

UTF-8 file test.txt:

UTF-8 文件 test.txt:

AAAAAAAAAAAAAA

hex is

十六进制是

41 41 41 41 41 41 41 41 41 41

sed s/A/B/g test.txtworks

sed s/A/B/g test.txt作品

sed s/\x41/B/g test.txtdoes not work

sed s/\x41/B/g test.txt不起作用

Some characters are unprintable so I must use their hex, Ais just an example.

有些字符是不可打印的,所以我必须使用它们的十六进制,A这只是一个例子。

采纳答案by Karoly Horvath

the shell preprocesses it, use single quotes.

shell 对其进行预处理,使用单引号。

sed 's/\x41/B/g' test.txt

echo -e \x41   # x41
echo -e '\x41' # A

回答by chrisdowney

If you only want to replace individual characters, you should be able to use tr with octal escapes like this:

如果您只想替换单个字符,您应该能够像这样使用八进制转义符 tr:

tr '1' B