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
sed replace with hex
提问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.txt
works
sed s/A/B/g test.txt
作品
sed s/\x41/B/g test.txt
does not work
sed s/\x41/B/g test.txt
不起作用
Some characters are unprintable so I must use their hex, A
is 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