转自:http://lorna8023.blog.51cto.com/777608/420313

用途说明

iconv命令是用来转换文件的编码方式的(Convert encoding of given files from one encoding to another),比如它可以将UTF8编码的转换成GB18030的编码,反过来也行。JDK中也提供了类似的工具native2ascii。Linux下的iconv开发库包括iconv_open,iconv_close,iconv等C函数,可以用来在C/C++程序中很方便的转换字符编码,这在抓取网页的程序中很有用处,而iconv命令在调试此类程序时用得着。

常用参数

首先,我们要知道支持的字符编码有哪些,这个可以用-l参数得到(List known coded character sets)。

格式:iconv -l

其次,是怎样转换,如下所示:

格式:iconv -f from-encoding -t to-encoding inputfile

上面的调用方式,会把输出打印在屏幕上,如果要输出到文件,可以像下面这样

格式:iconv -f from-encoding -t to-encoding inputfile -o outputfile

使用示例 示例一 列出支持的字符编码

[root@new55 ~]# iconv -l 
The following list contain all the coded character sets known.  This does 
not necessarily mean that all combinations of these names can be used for 
the FROM and TO command line parameters.  One coded character set can be 
listed with several different names (aliases). 
  437, 500, 500V1, 850, 851, 852, 855, 856, 857, 860, 861, 862, 863, 864, 865, 
  866, 866NAV, 869, 874, 904, 1026, 1046, 1047, 8859_1, 8859_2, 8859_3, 8859_4, 
  8859_5, 8859_6, 8859_7, 8859_8, 8859_9, 10646-1:1993, 10646-1:1993/UCS4, 
  ANSI_X3.4-1968, ANSI_X3.4-1986, ANSI_X3.4, ANSI_X3.110-1983, ANSI_X3.110, 
  ARABIC, ARABIC7, ARMSCII-8, ASCII, ASMO-708, ASMO_449, BALTIC, BIG-5, 
  BIG-FIVE, BIG5-HKSCS, BIG5, BIG5HKSCS, BIGFIVE, BS_4730, CA, CN-BIG5, CN-GB, 
中间省略掉输出了。 
  EUCJP-OPEN, EUCJP-WIN, EUCJP, EUCKR, EUCTW, FI, FR, GB, GB2312, GB13000, 
  GB18030, GBK, GB_1988-80, GB_198880, GEORGIAN-ACADEMY, GEORGIAN-PS, 
  GOST_19768-74, GOST_19768, GOST_1976874, GREEK-CCITT, GREEK, GREEK7-OLD, 
  GREEK7, GREEK7OLD, GREEK8, GREEKCCITT, HEBREW, HP-ROMAN8, HPROMAN8, HU, 
中间省略掉输出了。 
  TIS620.2529-1, TIS620.2533-0, TIS620, TS-5881, TSCII, UCS-2, UCS-2BE, 
  UCS-2LE, UCS-4, UCS-4BE, UCS-4LE, UCS2, UCS4, UHC, UJIS, UK, UNICODE, 
  UNICODEBIG, UNICODELITTLE, US-ASCII, US, UTF-7, UTF-8, UTF-16, UTF-16BE, 
  UTF-16LE, UTF-32, UTF-32BE, UTF-32LE, UTF7, UTF8, UTF16, UTF16BE, UTF16LE, 
  UTF32, UTF32BE, UTF32LE, VISCII, WCHAR_T, WIN-SAMI-2, WINBALTRIM, 
  WINDOWS-31J, WINDOWS-874, WINDOWS-936, WINDOWS-1250, WINDOWS-1251, 
  WINDOWS-1252, WINDOWS-1253, WINDOWS-1254, WINDOWS-1255, WINDOWS-1256, 
  WINDOWS-1257, WINDOWS-1258, WINSAMI2, WS2, YU

太多了,我只想知道支持哪些中文格式的。 
[root@new55 ~]# iconv -l | grep GB 
CN-GB// 
CSGB2312// 
CSISO58GB1988// 
EBCDIC-CP-GB// 
GB// 
GB2312// 
GB13000// 
GB18030// 
GBK// 
GB_1988-80// 
GB_198880// 
ISO646-GB//

有没有发现奇怪的地方,每行显示一个,并且后面加了两个斜杠。 
[root@new55 ~]#

示例二 将Google香港的Big5编码转换成GBK编码

[root@new55 ~]# curl -s http://www.google.com.hk/ | iconv -f big5 -t gbk 
<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=Big5"><title>Google</title><script>window.google={kEI:"tFXZTNHKDcGTkAXpvOHhCA",kEXPI:"26637,27404",kCSI:{e:"26637,27404",ei:"tFXZTNHKDcGTkAXpvOHhCA",expi:"26637,27404"},ml:function(){},kHL:"zh-TW",time:function(){return(new Date).getTime()},log:function(b,d,c){var a=new Image,e=google,g=e.lc,f=e.li;a.onerror=(a.onload=(a.onabort=function(){delete g[f]}));g[f]=a;c=c||"/gen_204?atyp=i&ct="+b+"&cad="+d+"&zx="+google.time();a.src=c;e.li=f+1},lc:[],li:0,Toolbelt:{}}; 
id=ghead&gt;<div id=gbar><nobr><b class="gb1">所有網頁</b> <a onclick=gbar.qs(this) href="http://www.google.com.hk/imghp?hl=zh-tw&tab=wi" class="gb1">圖片</a> <a onclick=gbar.qs(this) href="http://video.google.com.hk/?hl=zh-tw&tab=wv" class="gb1">影片</a> <a onclick=gbar.qs(this) href="http://maps.google.com.hk/maps?hl=zh-tw&tab=wl" class="gb1">地圖</a> <a onclick=gbar.qs(this) f||document.f||document.gs;google.ac.i(form,form.q,'','','',{o:1,sw:1});google.mc = [[14,{}],[64,{}],[105,{}],[22,{"m_error":"\u003Cfont color=red\u003E錯誤:\u003C/font\u003E 伺服器無法完成您的要求。  請在 30 秒後再試一次。","m_tip":"按一下以取得詳細資訊。"}],[84,{}]];google.med('init');google.History&&google.History.initialize('/')});if(google.j&&google.j.en&&google.j.xi){window.setTimeout(google.j.xi,0);google.fade=null;}&lt;/script></div><script>(function(){ 
中间省略掉输出了。 
})(); 
</script>[root@new55 ~]#

示例三 将我的JavaEye博客首页从UTF8转换成GBK

[root@new55 ~]# curl -s http://codingstandards.javaeye.com/ | iconv -f utf8 -t gbk  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"> 
  <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <title>Bash @ Linux - JavaEye技术网站</title> 
    <meta name="description" content="" /> 
    <meta name="keywords" content="codingstandards Bash @ Linux" /> 
中间省略掉输出了。 
<div class="blog_main"> 
<div class="blog_title"> 
<div class="date"><span class='year'>2010</span><span class='sep_year'>-</span><span class='month'>10</span><span class='sep_month'>-</span><span class='day'>17</span></div> 
<div class="show_full_flag"><a href='?show_full=true'>全文显示</a></div> 
<h3><a href='/blog/786653'>[置顶] 我使用过的Linux命令系列总目录</a></h3> 
<strong>文章分类:<a href="http://www.javaeye.com/blogs/category/os" style="text-decoration:none;padding-right:10px;">操作系统</a></strong> 
</div> 
<div class="blog_content"> 
    我使用过的Linux命令系列总目录 
本文链接: http://codingstandards.javaeye.com/blog/786653 
iconv: 未知 3345 处的非法输入序列

最后一行表明有错,改用下面的就会成功了。 
[root@new55 ~]# curl -s http://codingstandards.javaeye.com/ | iconv -f utf8 -t gb18030

此处省略输出。有兴趣的读者可以试一下,可以完整的显示整个页面的源代码。因为gbk是gb18030的子集,gb18030包含更多的字符。

[root@new55 ~]#

示例四 将梦之都的UTF8转换成GBK

[root@new55 ~]# curl -s http://www.dreamdu.com/ | iconv -futf8 -t gbk 
iconv: 未知 0 处的非法输入序列

有问题,用hexdump来看一下里面的字节,发现里面有ef bb bf的BOM信息,iconv不支持。 
[root@new55 ~]# curl -s http://www.dreamdu.com/ | hexdump -C | less 
00000000  ef bb bf 3c 21 44 4f 43  54 59 50 45 20 68 74 6d  |...<!DOCTYPE htm| 
00000010  6c 20 50 55 42 4c 49 43  20 22 2d 2f 2f 57 33 43  |l PUBLIC "-//W3C| 
00000020  2f 2f 44 54 44 20 58 48  54 4d 4c 20 31 2e 30 20  |//DTD XHTML 1.0 | 
00000030  53 74 72 69 63 74 2f 2f  45 4e 22 20 22 68 74 74  |Strict//EN" "htt| 
00000040  70 3a 2f 2f 77 77 77 2e  77 33 2e 6f 72 67 2f 54  |p://www.w3.org/T| 
00000050  52 2f 78 68 74 6d 6c 31  2f 44 54 44 2f 78 68 74  |R/xhtml1/DTD/xht| 
00000060  6d 6c 31 2d 73 74 72 69  63 74 2e 64 74 64 22 3e  |ml1-strict.dtd">| 
00000070  0d 0a 3c 68 74 6d 6c 20  78 6d 6c 6e 73 3d 22 68  |..<html xmlns="h|

:q

那就把前面三个字节去掉试试,果然可以了。

[root@new55 ~]# curl -s http://www.dreamdu.com/ | cut -b 4- | iconv -futf8 -t gbk 
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
ml xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"&gt; 
ead&gt; 
meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt; 
meta http-equiv="content-language" content="zh-CN" /&gt; 
link rel="stylesheet" type="text/css" href="/style.css?v=1" media="screen" /&gt; 
script type="text/javascript" src=\'#\'" /js.js"&gt;</script> 
title&gt;梦之都 - 网站设计与开发教程</title> 
head&gt; 
ody&gt;

中间省略掉输出。 
body&gt; 
tml&gt;

发现问题没有,每行的前面几个字符都消失了!!! 
[root@new55 ~]#

[转]Linux命令之iconv的更多相关文章

  1. Linux命令之乐--iconv

    用法: 实际应用: 批量转换文件编码: [root@wls12c PCK]$ for tfile in `ls -l|awk '{print $9}'`;do echo "iconv -f ...

  2. linux常用命令:iconv 命令

    iconv命令是linux下用于文件转编码的常用命令,对于同时使用windows系统和linux系统的同学来说文件转编码也是经常遇到的操作. 1.命令格式: iconv [选项...] [文件...] ...

  3. Linux命令整理中...

    Linux命令整理中... 最常用命令(我最近最常用的一般放在前面tipsbychsry) clear 清屏 date 显示日期 cal 显示日历 cal 2014 显示2014年的日历 shutdo ...

  4. 最有用的Linux命令行使用技巧集锦

    最近在Quora上看到一个问答题目,关于在高效率Linux用户节省时间Tips.将该题目的回答进行学习总结,加上自己的一些经验,记录如下,方便自己和大家参考. 下面介绍的都是一些命令行工具,这些工具在 ...

  5. 在windows下使用linux命令

    <转:http://www.cnblogs.com/adgnat/archive/2011/07/16/2108098.html> 使用过linxu的伙计估计都会喜欢上linux各种各样强 ...

  6. Linux命令行技巧

    Linux命令行技巧 命令 描述 • apropos whatis 显示和word相关的命令. 参见线程安全 • man -t man | ps2pdf - > man.pdf 生成一个PDF格 ...

  7. Linux命令之dot - 绘制DOT语言脚本描述的图形

    本文链接:http://codingstandards.iteye.com/blog/840055 用途说明 Graphviz (Graph Visualization Software的缩写)是一个 ...

  8. 【改造Linux命令之rm - 删除文件或目录-】

    用途说明 rm命令是常用的命令,用来删除文件或目录(remove files or directories).它也是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比 ...

  9. 十年linux命令总结

    十年linux命令总结 本文链接: http://codingstandards.iteye.com/blog/786653 关于命令类型划分 本表中列出了我穷尽了我所有的记忆整理出来的Linux命令 ...

随机推荐

  1. 【03】AngularJS 简介

    AngularJS 简介 AngularJS 是一个 JavaScript 框架.它可通过 <script> 标签添加到 HTML 页面. AngularJS 通过 指令 扩展了 HTML ...

  2. Codeforces Round #234 (Div. 2)

    A. Inna and Choose Options time limit per test 1 second memory limit per test 256 megabytes input st ...

  3. SpringBoot @ConditionalOnBean、@ConditionalOnMissingBean注解源码分析与示例

    前言: Spring4推出了@Conditional注解,方便程序根据当前环境或者容器情况来动态注入bean,对@Conditional注解不熟悉的朋友可移步至 Spring @Conditional ...

  4. 【eclipse】eclipse启动优化&打印GC信息&重要的堆结构连接

    eclipse启动优化,终于不那么卡了! 网上找了好多都是myEclipse的优化的,跟eclipse有点区别,找了很多方法还是不能让这个eclipse(Version: Kepler Release ...

  5. Android GIS开发系列-- 入门季(6)GraphicsLayer添加文字与图片标签

    一.GraphicsLayer添加图片 GraphicLayer添加图片Graphic,要用到PictureMarkerSymbol,也是样式的一种.添加代码如下: Drawable drawable ...

  6. 奇偶数对调,保持顺序 —— 剑指Offer

    这道题目 https://www.nowcoder.net/practice/beb5aa231adc45b2a5dcc5b62c93f593?tpId=13&tqId=11166&t ...

  7. [Vue + TS] Watch for Changes in Vue Using the @Watch Decorator with TypeScript

    Vue watchers allow to perform async updates as a side effect of a property change. This lesson shows ...

  8. UVA 10555 - Dead Fraction(数论+无限循环小数)

    UVA 10555 - Dead Fraction 题目链接 题意:给定一个循环小数,不确定循环节,求出该小数用分数表示,而且分母最小的情况 思路:推个小公式 一个小数0.aaaaabbb... 表示 ...

  9. git-svn for mac

    熟练使用 git ,新公司用的是 svn,这就尴尬了,为了这个习惯问题,我还是毅然坚持使用 git,但是又不与公司的 svn 冲突,所以就找到了 git 的 git-svn 插件. 在 mac 上使用 ...

  10. Spring MVC学习-------------訪问到静态的文件

    怎样訪问到静态的文件,如jpg,js,css? 怎样你的DispatcherServlet拦截"*.do"这种有后缀的URL.就不存在訪问不到静态资源的问题. 假设你的Dispat ...