使用場景:
在 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的使用比較有限,之後再進行補充