iconv命令是运行于linux/unix平台的文件编码装换工具。当我们在linux/unix系统shell查看文本文件时,常常会发现文件的中文是乱码的,这是由于文本文件的编码与当前操作系统设置的编码不同而引起的,这时可以使用iconv进行编码转换,从而解决乱码问题。 解决文本文件乱码问题分3步:1.确定文件编码,2.确定iconv是否支持此编码的转换,3.确定Linux/Unix操作系统编码,4.转换文件编码为与系统编码一致;下面通过对test.txt文件来举例。


1、 使用file命令来确定文件编码:

$ file -bi gbk.txt | sed -e 's/.*[ ]charset=//' |tr '[a-z]' '[A-Z'
ISO-8859-1

可见test.txt文件编码为ISO-8859-1编码。


2、 使用iconv -l确定iconv是否支持此种编码的转换:

$ iconv -l | grep ISO-8859-1
ISO-8859-1//
ISO-8859-10//
ISO-8859-11//
ISO-8859-13//
ISO-8859-14//
ISO-8859-15//
ISO-8859-16//


3、 确定Linux/Unix操作系统编码:

$ echo $LANG
zh_CN.UTF-8

当前操作系统坏境编码为"UTF-8"


4、 转换编码

$ iconv -f ISO-8859-1 -t UTF-8 test.txt
测试

注:由于file命令常常会误判编码,如发现转换出来的编码依然是乱码,可将iconv -f的输入编码换成其他常用编码试试: GBK、BIG5、HZ、GB2312、GB18030、ASCII


iconv命令的详细语法:

iconv [选项..] 文件 

 选项:

-f 输入编码 -t 输出编码 -l 列出所有已知的编码 -o 输出文件


附录字符编码表:

编码集:

ISO-8859-2ISO 8859-2 standard; ISO Latin 2
ISO-8859-4ISO 8859-4 standard; Latin 4
ISO-8859-5ISO 8859-5 standard; ISO Cyrillic
ISO-8859-13ISO 8859-13 standard; ISO Baltic; Latin 7
ISO-8859-16ISO 8859-16 standard
CP1125MS-Windows code page 1125
CP1250MS-Windows code page 1250
CP1251MS-Windows code page 1251
CP1257MS-Windows code page 1257; WinBaltRim
IBM852IBM/MS code page 852; PC (DOS) Latin 2
IBM855IBM/MS code page 855
IBM775IBM/MS code page 775
IBM866IBM/MS code page 866
balticISO-IR-179; Baltic
KEYBCS2Kamenicky encoding; KEYBCS2
macceMacintosh Central European
maccyrMacintosh Cyrillic
ECMA-113Ecma Cyrillic; ECMA-113
KOI-8_CS_2KOI8-CS2 code ('T602')
KOI8-RKOI8-R Cyrillic
KOI8-UKOI8-U Cyrillic
KOI8-UNIKOI8-Unified Cyrillic
TeX(La)TeX control sequences
UCS-2Universal character set 2 bytes; UCS-2; BMP
UCS-4Universal character set 4 bytes; UCS-4; ISO-10646
UTF-7Universal transformation format 7 bits; UTF-7
UTF-8Universal transformation format 8 bits; UTF-8
CORKCork encoding; T1
GBKSimplified Chinese National Standard; GB2312
BIG5Traditional Chinese Industrial Standard; Big5
HZHZ encoded GB2312


行结束符:

/LFLF line terminators
/CRLFCRLF line terminators
N.A.Mixed line terminators
N.A.Surrounded by/intermixed with non-text data
/21Byte order reversed in pairs (1,2 -> 2,1)
/4321Byte order reversed in quadruples (1,2,3,4 -> 4,3,2,1)
N.A.Both little and big endian chunks, concatenated
/qpQuoted-printable encoded


各国语言编码集:

BulgarianCP1251 ISO-8859-5 IBM855 maccyr ECMA-113
CzechISO-8859-2 CP1250 IBM852 KEYBCS2 macce KOI-8_CS_2 CORK
EstonianISO-8859-4 CP1257 IBM775 ISO-8859-13 macce baltic
CroatianCP1250 ISO-8859-2 IBM852 macce CORK
HungarianISO-8859-2 CP1250 IBM852 macce CORK
LithuanianCP1257 ISO-8859-4 IBM775 ISO-8859-13 macce baltic
LatvianCP1257 ISO-8859-4 IBM775 ISO-8859-13 macce baltic
PolishISO-8859-2 CP1250 IBM852 macce ISO-8859-13 ISO-8859-16 baltic CORK
RussianKOI8-R CP1251 ISO-8859-5 IBM866 maccyr
SlovakCP1250 ISO-8859-2 IBM852 KEYBCS2 macce KOI-8_CS_2 CORK
SloveneISO-8859-2 CP1250 IBM852 macce CORK
UkrainianCP1251 IBM855 ISO-8859-5 CP1125 KOI8-U maccyr
ChineseGBK BIG5 HZ