我使用下面的程式碼將“\u00c0”等字元轉換為“À”等 Unicode 格式
unicode(){ sed -i 's/\\\u00c0/À/g' $1;sed -i 's/\\\u00c1/Á/g' $1;sed -i 's/\\\u00c2/Â/g' $1;sed -i 's/\\\u00c3/Ã/g' $1;sed -i 's/\\\u00c4/Ä/g' $1;sed -i 's/\\\u00c5/Å/g' $1;sed -i 's/\\\u00c6/Æ/g' $1;sed -i 's/\\\u00c7/Ç/g' $1;sed -i 's/\\\u00c8/È/g' $1;sed -i 's/\\\u00c9/É/g' $1;sed -i 's/\\\u00ca/Ê/g' $1;sed -i 's/\\\u00cb/Ë/g' $1;sed -i 's/\\\u00cc/Ì/g' $1;sed -i 's/\\\u00cd/Í/g' $1;sed -i 's/\\\u00ce/Î/g' $1;sed -i 's/\\\u00cf/Ï/g' $1;sed -i 's/\\\u00d0/Ð/g' $1;sed -i 's/\\\u00d1/Ñ/g' $1;sed -i 's/\\\u00d2/Ò/g' $1;sed -i 's/\\\u00d3/Ó/g' $1;sed -i 's/\\\u00d4/Ô/g' $1;sed -i 's/\\\u00d5/Õ/g' $1;sed -i 's/\\\u00d6/Ö/g' $1;sed -i 's/\\\u00d7/×/g' $1;sed -i 's/\\\u00d8/Ø/g' $1;sed -i 's/\\\u00d9/Ù/g' $1;sed -i 's/\\\u00da/Ú/g' $1;sed -i 's/\\\u00db/Û/g' $1;sed -i 's/\\\u00dc/Ü/g' $1;sed -i 's/\\\u00dd/Ý/g' $1;sed -i 's/\\\u00de/Þ/g' $1;sed -i 's/\\\u00df/ß/g' $1;sed -i 's/\\\u00e0/à/g' $1;sed -i 's/\\\u00e1/á/g' $1;sed -i 's/\\\u00e2/â/g' $1;sed -i 's/\\\u00e3/ã/g' $1;sed -i 's/\\\u00e4/ä/g' $1;sed -i 's/\\\u00e5/å/g' $1;sed -i 's/\\\u00e6/æ/g' $1;sed -i 's/\\\u00e7/ç/g' $1;sed -i 's/\\\u00e8/è/g' $1;sed -i 's/\\\u00e9/é/g' $1;sed -i 's/\\\u00ea/ê/g' $1;sed -i 's/\\\u00eb/ë/g' $1;sed -i 's/\\\u00ec/ì/g' $1;sed -i 's/\\\u00ed/í/g' $1;sed -i 's/\\\u00ee/î/g' $1;sed -i 's/\\\u00ef/ï/g' $1;sed -i 's/\\\u00f0/ð/g' $1;sed -i 's/\\\u00f1/ñ/g' $1;sed -i 's/\\\u00f2/ò/g' $1;sed -i 's/\\\u00f3/ó/g' $1;sed -i 's/\\\u00f4/ô/g' $1;sed -i 's/\\\u00f5/õ/g' $1;sed -i 's/\\\u00f6/ö/g' $1;sed -i 's/\\\u00f7/÷/g' $1;sed -i 's/\\\u00f8/ø/g' $1;sed -i 's/\\\u00f9/ù/g' $1;sed -i 's/\\\u00fa/ú/g' $1;sed -i 's/\\\u00fb/û/g' $1;sed -i 's/\\\u00fc/ü/g' $1;sed -i 's/\\\u00fd/ý/g' $1;sed -i 's/\\\u00fe/þ/g' $1;sed -i 's/\\\u00ff/ÿ/g' $1; }
然後我用它unicode file.txt
來轉換為 Unicode。
例如,如果我有一個名為 Original_text 的文件,並且它有一個像 \u00d8rsted 這樣的字串,則 rununicode original_text
會將該字串轉換為Ørsted
.
這工作得很好,但代碼似乎很不正確,實際上看起來有點難看。
我想知道,是否有更好的方法來進行這種轉換(在 shell 甚至 unix 命令上來轉換此類字元)?
答案1
ascii2uni
從uni2ascii可以做到這一點。
$ ./ascii2uni -q -a U <<< '\u00d8rsted'
Ørsted