1. 程式人生 > >結合file和iconv命令轉換檔案的字元編碼型別

結合file和iconv命令轉換檔案的字元編碼型別

    總結一下:如果想把一個未知字元編碼型別的文字檔案用指定的編碼型別重新編碼,該怎麼辦呢?

    1. 用file命令檢視該檔案的字元編碼
    2. 通過iconv -l確認iconv是否支援該編碼型別,如果支援,從中找出一個最接近的試試
    3. 如果可以,那麼啟用iconv進行轉換,否則提示錯誤

    這樣就可以寫一個指令碼來自動進行這個轉換過程了(不完善,可以自己新增一些內容),例如:

Code:
#!/bin/bash
#encode.sh -- encode a file with an indicated encoding

# make sure user give two arguments

[ "$#" != 2 ] && echo "Usage: `basename $0` [to_encoding] [file]" && exit -1

# make sure the second argument is a regular file

[ ! -f $2 ] && echo "the second argument should be a regular file " && exit 1
file=$2

# make sure the first argument is a encoding supported by iconv

iconv -l | grep -q $1
[ $? -ne 0 ] && echo "iconv not support such encoding: $1" && exit -1
to_encoding=$1

# is there a text file?
file_type=`file $file | grep "text$"`
[ $? -ne 0 ] && echo "$file is not a text file" && exit -1

# get the old encoding
from_encoding=`echo $file_type | cut -d" " -f 2`
from_encoding=`iconv -l | grep $from_encoding`
[ $? -ne 0 ] && echo "iconv not support the old encoding: $from_encoding"
from_encoding=`echo $from_encoding | cut -d"/" -f 1`

# convert the file from from_encoding to to_encoding
iconv -f $from_encoding -t $to_encoding $file

[Ctrl+A Select All]
    
    下載以後儲存為encode.sh,新增可執行許可權,並轉換一個檔案試試。
$ chmod +x encode.sh
$ ./encode.sh UTF8 kernel/sys.c