close

[螞蟻計畫] GNU/Linux 指令 iconv
指令名稱:
iconv(1) - 字碼轉換
語法:
iconv -f encoding -t encoding inputfile
說明:

這是一個轉換字碼工具。使用相當簡單。
選項:

--from-code, -f encoding
來源的編碼
--to-code, -t encoding
目的(輸出)的編碼
--list
顯示指令支援的所有編碼一覽
--output, -o file
將轉碼的輸出儲存到指定的檔案
--verbose
顯示程式本身的版本資訊

實例說明:

這個工具,農夫自己時常用它來做中文繁體(Big5)及簡體(gb2312)字碼的轉換。使用的方式相當簡單。來看下面這個例子
# iconv index.php -f big5 -t gb2312 -o index-gb.html

將 big5 編碼的 index.php 轉為 gb2312 編碼並將輸出結果存到 index-gb.html 檔案中。有了她,繁簡互轉變得簡單許多。這篇簡單到此。

網路農夫 2004/09/04

引用:http://www.ubuntu-tw.org/modules/newbb/viewtopic.php?topic_id=7337&viewmode=compact&order=ASC&type=&mode=0&start=0

 

#!/bin/bash
#iconv_GB2312toBIG5
#以遞迴的方式深入目錄,轉換所有TXT檔案的字元編碼。
#MOSKY 09/05/29
function START()
{
mkdir converted
for fileName in `ls -F | grep '.txt'` #找尋資料夾內所有的TXT檔案
do
echo ' Converting: "'$fileName'"...'; #輸出轉換中訊息
iconv "$fileName" -f gb2312 -t big5 -o ./converted/"$fileName" #轉換指令
done;

for folderName in `ls -F | grep '/'` #深入子目錄
do
folderName=${folderName%/*}; #稍微修正目錄表示方式,顯示比較美觀
if [ $folderName != "converted" ]
then
echo ' Into: "'$folderName'".'; #輸出訊息
cd "$folderName"; #移動至該目錄
START; #遞迴呼叫
cd ..; #子目錄完畢後回原目錄
fi
done;
}
read -p 'Please pick a DIR:' dir; #輸入要進行作業的資料夾名稱
cd "$dir"; #切換到下層目錄
echo 'Here we go!'; #程序開始訊息
START; #執行遞迴函數
echo 'Done!'; #完成訊息

 

 

 

  • 5.8.0版開始,Perl具備了Unicode萬國碼)支援
    • 將Big5編碼的檔案轉成Unicode,祗需鍵入下列指令:
perl -Mencoding=big5,STDOUT,utf8 -pe1 < file.big5 > file.utf8
    • Perl也內附了piconv,一個完全以Perl寫成的字碼轉換工具程式,用法如下:
piconv -f big5 -t utf8 < file.big5 > file.utf8
piconv -f utf8 -t big5 < file.utf8 > file.big5

 

arrow
arrow
    全站熱搜

    鳥人 發表在 痞客邦 留言(0) 人氣()