使用场景:
在 linux 平台上对文件格式进行转换,比如将GBK格式的文件转换为UTF8格式
转换方法
1. 使用 iconv
使用 man 查看 iconv 的帮助命令,比较简单
NAME
iconv - Convert encoding of given files from one encoding to another
SYNOPSIS
iconv -f encoding -t encoding inputfile
DESCRIPTION
The iconv program converts the encoding of characters in inputfile
from one coded character set to another. The result is written to
standard output unless otherwise specified by the --output option.
--from-code, -f encoding
Convert characters from encoding
--to-code, -t encoding
Convert characters to encoding
--list
List known coded character sets
--output, -o file
Specify output file (instead of stdout)
--verbose
Print progress information.
下面将gbk格式的文件转换utf8格式,命令为:
iconv -f gbk -t utf8 oldFile > newFile
2. 使用 enca
enca相比于iconv的优势在于它可以获得文件的编码格式,这样就可以在不必知道文件格式的情况下将文件转换为想要的格式
enca不是系统自带的,需要安装,安装过程如下:
#下载&解压
wget http://dl.cihar.com/enca/enca-1.13.tar.gz #可以自己去找最新版
tar -zxvf enca-1.13.tar.gz
cd enca-1.13
#编译安装
./configure
make
make install
enca的用法
enca -L zh_CN file #查看file的编码格式
enca -L zh_CN -x UTF-8 file #将file转换成utf8的编码格式,覆盖原来的文件
enca -L zh_CN -x UTF-8 < file1 > file2 #转换之后存成file2文件,不覆盖原来的文件
目前对enca的使用比较有限,之后再进行补充