iconv-转换文件编码

iconv命令是用来转换文件的编码方式的,比如它可以将UTF8编码的转换成GB18030的编码,反过来也行。

iconv -f encoding \[-t encoding\] \[inputfile\]...
参数
描述

-f encoding

把字符从encoding编码开始转换。

-t encoding

把字符转换到encoding编码。

-l

列出已知的编码字符集合

-o file

指定输出文件

-c

忽略输出的非法字符

-s

禁止警告信息,但不是错误信息

--verbose

显示进度信息

-f和-t所能指定的合法字符在-l选项的命令里面都列出来了。

示例:

# 批量修改文件编码格式
for filename in `ls`;do iconv -f UTF-8 -t GBK $filename -o $filename;done

# 单个文件转换
iconv -f UTF-8 -t GBK filename -o filename

错误处理

iconv -f UTF-8 -t GBK /tmp/jsc.jsc_org_customer.txt -o /tmp/jsc.jsc_org_customer.2.txt iconv: illegal input sequence at position 832413

使用-c参数忽略输出的非法字符

iconv -c -f UTF-8 -t GBK /tmp/jsc.jsc_org_customer.txt -o /tmp/jsc.jsc_org_customer.2.txt

最后更新于