0%

iconv 指令

介紹

iconv 是 linux 上字元轉換的工具。因為有很多機會需要把簡體中文轉換為繁體中文。所以 google 介紹了這個好用的工具。

iconv 把指定的檔案從來源字集轉換到目的字集。以我的應用場景,我是需要從簡體中文轉換到繁體中文。

可以查到簡體在 iconv 是 gb2312 而繁體中文是 big5。但是以現在的作業系統來說我們的字集一般是 utf8。

所以實作的操作是先從 utf8 轉換為 gb2312 , 指後再從 gb2312 轉換為 big5 ,最後再從 big5 再轉回 utf8。

使用

列出目前支援的字集。

1
2
3
4
5
6
7
8
9
10
11
 $ iconv -l
The following list contains 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).

437, 500, 500V1, 850, 851, 852, 855, 856, 857, 858, 860, 861, 862, 863, 864,
865, 866, 866NAV, 869, 874, 904, 1026, 1046, 1047, 8859_1, 8859_2, 8859_3,
8859_4, 8859_5, 8859_6, 8859_7, 8859_8, 8859_9, 10646-1:1993,
10646-1:1993/UCS4, ANSI_X3.4-1968, ANSI_X3.4-1986, ANSI_X3.4,
...

轉換字集

下面的例子是將檔案轉換為簡體。

1
$ iconv -f utf8 -t gb2312 file.txt

實際我們一般會如下進行轉換

1
$ cat file.txt | iconv -f utf8 -t gb2312 | iconv -f gb2312 -t big5 | iconv -f big5 -t utf8