Chinaunix首页 | 论坛 | 博客
  • 博客访问: 776889
  • 博文数量: 94
  • 博客积分: 1238
  • 博客等级: 少尉
  • 技术积分: 1174
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-13 19:22
个人简介

走自己的路让别人说去吧!

文章分类

全部博文(94)

文章存档

2015年(4)

2014年(3)

2013年(12)

2012年(32)

2011年(43)

分类:

2012-12-24 12:33:56

Linux对中文的支持不是很好,
也不像Windows样,会对文件名,文件内容做字符集的自动转换。

例如,将Windows下的文件复制到Linux下,会出现一堆的乱码,
这时,就要用到linux的一些字符集转换工具来处理。

1. 批量文件名字符集转换工具 -- convmv
下载链接:

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
  9. ...

则需要更改它的配置选项为:
  1. $./configure --enable-shared=no

原因详解:

使用示例:
  1. enca -L zh_CN test.sql                      // 检查文件编码
  2. enca -L zh_CN -x UTF-8 test.sql             // 将文件编码转换为UTF-8编码   
  3. enca -L zh_CN -x UTF-8 <test.sql> test2.sql // 转换并另存为test2.sql
阅读(719) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~