55

如何用sed进行十六进制替换

 5 years ago
source link: https://www.linuxprobe.com/sed-16-replace.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

现网teastore导出的数据文件,通过vim和less查看时,都发现是乱码,通过more查看时,部分内容不能正常显示,通过cat命令查看时会把相应的乱码部分自动处理掉。某业务人员想通过 fileio进行读取操作时,取的内容和vim 、less看到的效果一样,因为乱码位会占用一位。具体见下图:

正常复制上面的乱码符号,通过sed进行无法正常替换。此时可以换用16进制的替换,先用hexdump -C 命令进行查看,具体如下:

通过上面可以找到相应的乱码对应的十六制是多少。在通过sed替换时,通过在替换16进制数字前增加\x即可。类似命令如下:

sed -i 's/\x40\x1f//g' 1.test
sed -i 's/\x1f//g' 1.test
sed -i 's/\x00\x00\x00\x01//g' 1.test
sed -i 's/\x00\x00\x00\x03//g' 1.test

替换完成后,再查看就正常了:

[root@read1-sd tmp]# less 1.test
0|804690908461101|31205340930||80469090846@
0|804205542061101|30607176423||80420554206@
0|804571381121101|32017234631||80457138112@
0|801784713021101|41049442587||80178471302@
0|802643435491101|40081855639||80264343549@

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK