繁体   English   中英

如何用 tr 或 sed 将另一个文件中的“Ñ”替换为字符串?

[英]how replace “Ñ” in another file with strings, with tr or sed?

我有一个带有这个字符串的“file.txt”并显示特殊字符然后替换

我的 file.txt https://drive.google.com/file/d/1pTh4rxdlmA3Qq0aVwF5jCTK3StqXrQeJ/view?usp=sharing

12,IBA�EZ JUAN,2006,00030,NUEVO

字符 � 我知道是“Ñ”

我要这个

12,IBAÑEZ JUAN,2006,00030,NUEVO

我努力了

tr '\0xd1' 'Ñ' < file.txt  > file_2.txt 

我的 xxd 是

$ hexdump -C file.txt

00000000  31 32 2c 49 42 41 ef bf  bd 45 5a 20 4a 55 41 4e  |12,IBA...EZ JUAN|
00000010  2c 32 30 30 36 2c 30 30  30 33 30 2c 4e 55 45 56  |,2006,00030,NUEV|
00000020  4f 2c 00 2c 20 20 20 20  20 20 20 20 20 20 2c 4e  |O,.,          ,N|
00000030  4f 2c 00 2c 30 30 36 2c  50 2c 37 2e 30 30 30 2c  |O,.,006,P,7.000,|
00000040  2e 30 30 30 2c 31 32 2e  37 34 2c 2e 30 30 30 2c  |.000,12.74,.000,|
00000050  2d 2c 32 30 30 36 2d 30  36 2d 33 30              |-,2006-06-30|
0000005c

使用 hexdump,我们发现我的文件在一开始就延迟了 3 个冗余字节。

cat file.txt | hexdump -C

12,IBA�EZ JUAN,2006,00030,NUEVO, ,          ,NO, ,006,P,7.000,.000,12.74,.000,-,2006-06-30

将 cat output 连接到tr命令上,

cat file.txt | tr -s "�" "Ñ"

$ cat file.txt | hexdump -C
00000000  ef bb bf 31 32 2c 49 42  41 ef bf bd 45 5a 20 4a  |...12,IBA...EZ J|
00000010  55 41 4e 2c 32 30 30 36  2c 30 30 30 33 30 2c 4e  |UAN,2006,00030,N|
00000020  55 45 56 4f 2c 20 2c 20  20 20 20 20 20 20 20 20  |UEVO, ,         |
00000030  20 2c 4e 4f 2c 20 2c 30  30 36 2c 50 2c 37 2e 30  | ,NO, ,006,P,7.0|
00000040  30 30 2c 2e 30 30 30 2c  31 32 2e 37 34 2c 2e 30  |00,.000,12.74,.0|
00000050  30 30 2c 2d 2c 32 30 30  36 2d 30 36 2d 33 30     |00,-,2006-06-30|
0000005f

如果需要,再次检查 hexdump 以检查更改。原始文本有 3 个不可打印的字符,现在替换为 2 个字符: Ñ 检查下面的屏幕截图:

终端会话

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM