天天看點

linux進行中文字元集工具集粹與詳解

Linux對中文的支援不是很好,

也不像Windows樣,會對檔案名,檔案内容做字元集的自動轉換。

例如,将Windows下的檔案複制到Linux下,會出現一堆的亂碼,

這時,就要用到linux的一些字元集轉換工具來處理。

1. 批量檔案名字元集轉換工具 -- convmv

下載下傳連結:

http://download.chinaunix.net/download/0002000/1760.shtml

convmv是一個更改檔案名編碼方式的工具,

它甚而可以進行目錄下檔案名的批量轉換。

例如,

将/home目錄下原來檔案名是gbk編碼方式的全部改為utf-8格式的,

使用指令如下:

  1. $./convmv -f gbk -t utf8 -r –notest /home

-f 檔案名原編碼方式;

-t 檔案名要更改為的編碼方式;

-r 這個目錄下面的所有檔案;

-notest 表示馬上執行,而不是僅僅測試而已。

更多的功能詳解可見它的幫助:

  1. USAGE: convmv [options] FILE(S)
  2. -f enc encoding *from* which should be converted
  3. -t enc encoding *to* which should be converted
  4. -r recursively go through directories
  5. -i interactive mode (ask for each action)
  6. --nfc target files will be normalization form C for UTF-8 (Linux etc.)
  7. --nfd target files will be normalization form D for UTF-8 (OS X etc.)
  8. --qfrom be quiet about the "from" of a rename (if it screws up your terminal e.g.)
  9. --qto be quiet about the "to" of a rename (if it screws up your terminal e.g.)
  10. --exec c execute command instead of rename (use #1 and #2 and see man page)
  11. --list list all available encodings
  12. --lowmem keep memory footprint low (see man page)
  13. --nosmart ignore if files already seem to be UTF-8 and convert if posible
  14. --notest actually do rename the files
  15. --replace will replace files if they are equal
  16. --unescape convert%20ugly%20escape%20sequences
  17. --upper turn to upper case
  18. --lower turn to lower case
  19. --help print this help

2.  檔案内容字元集轉換工具 -- iconv

它通常是Linux系統自帶的轉換工具。

  1. $iconv -f gbk -t utf8 -o outfile infile

-f 檔案原來的編碼方式;

-t 輸出檔案的編碼方式; 

-o 輸出檔案名,這利用outfile表示,

最後跟上要更改編碼方式的檔案名infile 

這個工具有兩個缺陷:

一個是必須先知道檔案原來的編碼方式, 否則将出錯。

    這個可以用指令 "file filename" 得到。

二個是如果轉換出錯,将不會傳回。

iconv的用法:

  1. iconv指令用于轉換指定檔案的編碼,預設輸出到标準輸出裝置,亦可指定輸出檔案。
  2. 用法:iconv [選項...] [檔案...]
  3. 有如下選項可用:
  4. 輸入/輸出格式規範:
  5. -f, --from-code=名稱 原始文本編碼
  6. -t, --to-code=名稱 輸出編碼
  7. 資訊:
  8. -l, --list 列舉所有已知的字元集
  9. 輸出控制:
  10. -c 從輸出中忽略無效的字元
  11. -o, --output=FILE 輸出檔案
  12. -s, --silent 關閉警告
  13. --verbose 列印進度資訊
  14. --help 給出該系統求助清單
  15. --usage 給出簡要的用法資訊
  16. -V, --version 列印程式版本号

3. 強大的檔案内容字元集轉換工具 -- enca

下載下傳位址:

  1. $wget http://dl.cihar.com/enca/enca-1.13.tar.gz

在應用上enca比iconv更完善,在中文支援上enca比iconv支援得好,

iconv當遇到不支援的中文時會跳過或者報錯cannot iconving。

是以推薦用enca。

編譯與安裝:

  1. $tar -jxvf enca-1.13
  2. $cd enca-1.13
  3. $./configure
  4. $make
  5. $make check
  6. $make install

編譯時的出錯解決:

如果編譯時遇到出錯提示:

  1. ...
  2. /usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/libm.a(w_exp.o):
  3. relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object;
  4. recompile with -fPIC
  5. /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/libm.a: could not read symbols:
  6. Bad value
  7. collect2: ld returned 1 exit status
  8. make[2]: *** [libenca.la] Error 1

則需要更改它的配置選項為:

  1. $./configure --enable-shared=no

原因詳解:

http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3

使用示例:

  1. enca -L zh_CN test.sql                      // 檢查檔案編碼