福彩机选号码能中奖吗:结合file和iconv命令转换文件的字符编码类型

来源:百度文库 编辑:中财网 时间:2024/05/08 17:36:50

在很多类unix平台上都有一个iconv工具,可以用来转换字符编码;而对于普通的文本文件,file命令可以用来检测某个文件的字符编码类型,结合两者就可以非常方便地把一个未知编码类型的文本文件用某一指定编码类型进行编码。

    例如,linux内核源代码的某些文件编码就不是用ASCII编码的(貌似跟有些hacker“怪样”的姓名有关),例如:
$ cd /path/to/linux-2.6.17
$ file kernel/sys.c 
kernel/sys.c: ISO-8859 C program text

    可见这个文件的字符编码类型是ISO-8859。
    看看里头有哪些不是ASCII编码的,用iconv试着从ASCII转一下:
$ iconv -f ASCII -t UTF8 kernel/sys.c > /tmp/sys.c
iconv: illegal input sequence at position 29203



    发现转换出错了,在29203字节位置的字符编码并非是ASCII,用hexdump和cat命令看看该位置到底是什么:
$ hexdump -C -n 10 -s 29203 kernel/sys.c
00007213  e5 20 73 76 65 6e 73 6b  61 2e                    |. svenska.|
0000721d
$ cat kernel/sys.c | grep svenska
* Samma p? svenska..



    估计这个应该是某个作者的名字吧。
    下面根据file命令告诉我们的编码类型ISO-8859进行转换,先得通过iconv -l查看iconv到底支持ISO-8859不?
$ iconv -l |  grep ISO-8859
ISO-8859-1//
ISO-8859-2//
ISO-8859-3//
ISO-8859-4//
ISO-8859-5//
ISO-8859-6//
ISO-8859-7//
ISO-8859-8//
ISO-8859-9//
ISO-8859-9E//
ISO-8859-10//
ISO-8859-11//
ISO-8859-13//
ISO-8859-14//
ISO-8859-15//
ISO-8859-16//



    明显支持,但并不直接支持ISO-8859,所以在转换时得选择其中的一个试试。
$ iconv -f ISO-8859-1 -t UTF8 kernel/sys.c > /tmp/sys.c



    再看看转换以后的文件大小和29203字节附近的内容:
$ ls -l kernel/sys.c /tmp/sys.c 
-rwxr-xr-x 1 falcon falcon 50359 2006-06-18 09:49 kernel/sys.c
-rw-r--r-- 1 falcon falcon 50360 2008-06-29 14:06 /tmp/sys.c
$ cat /tmp/sys.c | grep sven
* Samma p? svenska..
    总结一下:如果想把一个未知字符编码类型的文本文件用指定的编码类型重新编码,该怎么办呢?

    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