一、命令简介
日常工作中我们需要将windows生成的文件上传到Linux系统,有时候会因为编码问题出现显示乱码。例如我上传了一个csv文件到Linux服务器上,默认编码为GB2312,在Linux打开则会出现乱码,我们需要将文件进行编码转换。iconv命令对于给定文件把它的内容从一种编码转换成另一种编码。
二、使用示例
1、查看命令版本
[root@s145 tmp]# iconv -V
iconv (GNU libc) 2.17
Copyright © 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Ulrich Drepper.
2、获取命令帮助
[root@s145 tmp]# iconv --help
3、获取帮助简讯
[root@s145 tmp]# iconv --usage
Usage: iconv [-lcs?V] [-f NAME] [-t NAME] [-o FILE] [–from-code=NAME]
[–to-code=NAME] [–list] [–output=FILE] [–silent] [–verbose]
[–help] [–usage] [–version] [FILE…]
4、查看支持的编码
#支持的编码格式非常多,有上千种。
[root@s145 tmp]# iconv -l
The following list contain all the coded character sets known. This does
not necessarily mean that all combinations of these names can be used for
the FROM and TO command line parameters. One coded character set can be
listed with several different names (aliases).
…
WINDOWS-31J, WINDOWS-874, WINDOWS-936, WINDOWS-1250, WINDOWS-1251,
WINDOWS-1252, WINDOWS-1253, WINDOWS-1254, WINDOWS-1255, WINDOWS-1256,
WINDOWS-1257, WINDOWS-1258, WINSAMI2, WS2, YU
5、将GBK文件转换为UTF-8文件
#可以使用>重定向将转换结果输入到新文件,文件名不能与原文件相同。
[root@s145 tmp]# cat B.csv
,Ա,5,6,7,8,9,ʱ
1,1,ID0025,A41,A51,A61,A71,A81,A91,2000/2/8
2,2,ID0026,A42,A52,A62,A72,A82,A92,2000/2/9
3,3,ID0027,A43,A53,A63,A73,A83,A93,2000/2/10
4,4,ID0028,A44,A54,A64,A74,A84,A94,2000/2/11
5,5,ID0029,A45,A55,A65,A75,A85,A95,2000/2/12
6,6,ID0030,A46,A56,A66,A76,A86,A96,2000/2/13
7,7,ID0031,A47,A57,A67,A77,A87,A97,2000/2/14
8,8,ID0032,A48,A58,A68,A78,A88,A98,2000/2/15
9,9,ID0033,A49,A59,A69,A79,A89,A99,2000/2/16
10,10,ID0034,A50,A60,A70,A80,A90,A100,2000/2/17
11,11,ID0035,A51,A61,A71,A81,A91,A101,2000/2/18
12,12,ID0036,A52,A62,A72,A82,A92,A102,2000/2/19
13,13,ID0037,A53,A63,A73,A83,A93,A103,2000/2/20
[root@s145 tmp]# file B.csv
B.csv: ISO-8859 text, with CRLF line terminators
[root@s145 tmp]# iconv -f GBK -t UTF-8 B.csv > b.csv
[root@s145 tmp]# cat b.csv
序号,姓名,员工编号,信息4,信息5,信息6,信息7,信息8,信息9,入职时间
1,张三1,ID0025,A41,A51,A61,A71,A81,A91,2000/2/8
2,张三2,ID0026,A42,A52,A62,A72,A82,A92,2000/2/9
3,张三3,ID0027,A43,A53,A63,A73,A83,A93,2000/2/10
4,张三4,ID0028,A44,A54,A64,A74,A84,A94,2000
————————————————
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
添加我为好友,拉您入交流群!
请使用微信扫一扫!