歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux處理中文字符集工具集粹與詳解

linux處理中文字符集工具集粹與詳解

日期:2017/3/1 12:21:02   编辑:關於Linux
Linux對中文的支持不是很好,也不像Windows樣,會對文件名,文件內容做字符集的自動轉換。 例如,將Windows下的文件復制到Linux下,會出現一堆的亂碼,這時,就要用到linux的一些字符集轉換工具來處理。 1. 批量文件名字符集轉換工具 -- convmv 下載鏈接: http://download.chinaunix.net/download/0002000/1760.shtml convmv是一個更改文件名編碼方式的工具, 它甚而可以進行目錄下文件名的批量轉換。 例如, 將/home目錄下原來文件名是gbk編碼方式的全部改為utf-8格式的, 使用命令如下: $./convmv -f gbk -t utf8 -r –notest /home -f 文件名原編碼方式; -t 文件名要更改為的編碼方式; -r 這個目錄下面的所有文件; -notest 表示馬上執行,而不是僅僅測試而已。 更多的功能詳解可見它的幫助: USAGE: convmv [options] FILE(S) -f enc encoding *from* which should be converted -t enc encoding *to* which should be converted -r recursively go through directories -i interactive mode (ask for each action) --nfc target files will be normalization form C for UTF-8 (Linux etc.) --nfd target files will be normalization form D for UTF-8 (OS X etc.) --qfrom be quiet about the "from" of a rename (if it screws up your terminal e.g.) --qto be quiet about the "to" of a rename (if it screws up your terminal e.g.) --exec c execute command instead of rename (use #1 and #2 and see man page) --list list all available encodings --lowmem keep memory footprint low (see man page) --nosmart ignore if files already seem to be UTF-8 and convert if posible --notest actually do rename the files --replace will replace files if they are equal --unescape convert%20ugly%20escape%20sequences --upper turn to upper case --lower turn to lower case --help print this help 2. 文件內容字符集轉換工具 -- iconv 它通常是Linux系統自帶的轉換工具。 $iconv -f gbk -t utf8 -o outfile infile -f 文件原來的編碼方式; -t 輸出文件的編碼方式; -o 輸出文件名,這利用outfile表示, 最後跟上要更改編碼方式的文件名infile 這個工具有兩個缺陷: 一個是必須先知道文件原來的編碼方式, 否則將出錯。 這個可以用命令 "file filename" 得到。 二個是如果轉換出錯,將不會返回。 iconv的用法: iconv命令用於轉換指定文件的編碼,默認輸出到標准輸出設備,亦可指定輸出文件。 用法:iconv [選項...] [文件...] 有如下選項可用: 輸入/輸出格式規范: -f, --from-code=名稱 原始文本編碼 -t, --to-code=名稱 輸出編碼 信息: -l, --list 列舉所有已知的字符集 輸出控制: -c 從輸出中忽略無效的字符 -o, --output=FILE 輸出文件 -s, --silent 關閉警告 --verbose 打印進度信息 --help 給出該系統求助列表 --usage 給出簡要的用法信息 -V, --version 打印程序版本號 3. 強大的文件內容字符集轉換工具 -- enca 下載地址: $wget http://dl.cihar.com/enca/enca-1.13.tar.gz 在應用上enca比iconv更完善,在中文支持上enca比iconv支持得好, iconv當遇到不支持的中文時會跳過或者報錯cannot iconving。 所以推薦用enca。 編譯與安裝: $tar -jxvf enca-1.13 $cd enca-1.13 $./configure $make $make check $make install 編譯時的出錯解決: 如果編譯時遇到出錯提示: ... /usr/bin/ld: /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/libm.a(w_exp.o): relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/libm.a: could not read symbols: Bad value collect2: ld returned 1 exit status make[2]: *** [libenca.la] Error 1 ... 則需要更改它的配置選項為: $./configure --enable-shared=no 原因詳解: http://www.gentoo.org/proj/en/base/amd64/howtos/index.xml?part=1&chap=3 使用示例: enca -L zh_CN test.sql // 檢查文件編碼 enca -L zh_CN -x UTF-8 test.sql // 將文件編碼轉換為UTF-8編碼    enca -L zh_CN -x UTF-8 <test.sql> test2.sql // 轉換並另存為test2.sql
Copyright © Linux教程網 All Rights Reserved