iconv

将文件的编码从一个字符集编码转换为另一个.比如它可以将UTF8编码的转换成GB18030的编码,反过来也行

语法

  iconv [Option...] -f encoding -t encoding inputfile 
  iconv -l

选项


   -f encoding
   --from-code encoding
          把字符从encoding编码开始转换

   -t encoding
   --to-code encoding
          把字符转换到encoding编码

   --list
          列出已知的编码字符集合

   -o file
   --output file
          指定输出文件


控制转换问题的选项:

   -c     忽略输出的非法字符

   --unicode-subst=formatstring
          替换无法在目标中表示的Unicode字符
          使用由formatstring构造的占位符字符串进行编码

   --byte-subst=formatstring
          替换输入中在源编码中无效的字节 
          使用由给定格式字符串构造的占位符字符串

   --widechar-subst=formatstring
          替换输入中在源中无效的字符
          使用从给定构造的占位符字符串进行编码
          formatstring,应用于字节的值

控制错误输出的选项t:

   −s
   −−silent
	     禁止警告信息,但不是错误信息 

   --verbose
       显示进度信息

例子

将input.txt从ISO-8859-1转换为UTF-8并保存为output.txt

$ iconv -f ISO-8859-1 -t UTF-8 < input.txt > output.txt

用于将目录中的所有.HTML文件从Windows 1242转换为UTF8的脚本

#/bin/bash
LIST=`ls *.html`
for i in $LIST;
do iconv -f WINDOWS-1252 -t UTF8 $i -o $i."utf8";
mv $i."utf8" $i;
done

iconv 命令实例:

linux命令:iconv -f437 -tutf8 asciiart.nfo asciiart.nfo -tutf8 -f437 iconv iconv -f437 -tutf8 asciiart.nforootopen.com
linux命令:iconv -f437 -tutf8 asciiart.nfo asciiart.nfo -tutf8 -f437 iconv iconv -f437 -tutf8 asciiart.nforootopen.com
2018-01-04 16:39:48

linux命令:iconv --from-code=ISO-8859-1 --to-code=UTF-8 iso.txt > utf.txt utf.txt > iso.txt --to-code=UTF-8 --from-code=ISO-8859-1 iconv iconv --from-code=ISO-8859-1 --to-code=UTF-8 iso.txt > utf.txtrootopen.com
linux命令:iconv --from-code=ISO-8859-1 --to-code=UTF-8 iso.txt > utf.txt utf.txt > iso.txt --to-code=UTF-8 --from-code=ISO-8859-1 iconv iconv --from-code=ISO-8859-1 --to-code=UTF-8 iso.txt > utf.txtrootopen.com
2018-03-19 09:14:48

linux命令:iconv -f utf8 -t utf16 /path/to/file /path/to/file utf16 -t utf8 -f iconv iconv -f utf8 -t utf16 /path/to/filerootopen.com
linux命令:iconv -f utf8 -t utf16 /path/to/file /path/to/file utf16 -t utf8 -f iconv iconv -f utf8 -t utf16 /path/to/filerootopen.com
2018-05-30 16:56:48

linux命令:iconv -f UTF16LE -t UTF-8 < SOURCE | awk 'BEGIN { RS="\r\n";} { gsub("\n", "\r"); print;}' > TARGET TARGET > print;}' "\r"); gsub("\n", { RS="\r\n";} { 'BEGIN awk | SOURCE < UTF-8 -t UTF16LE -f iconv iconv -f UTF16LE -t UTF-8 < SOURCE | awk 'BEGIN { RS="\r\n";} { gsub("\n", "\r"); print;}' > TARGETrootopen.com
linux命令:iconv -f UTF16LE -t UTF-8 < SOURCE | awk 'BEGIN { RS="\r\n";} { gsub("\n", "\r"); print;}' > TARGET TARGET > print;}' "\r"); gsub("\n", { RS="\r\n";} { 'BEGIN awk | SOURCE < UTF-8 -t UTF16LE -f iconv iconv -f UTF16LE -t UTF-8 < SOURCE | awk 'BEGIN { RS="\r\n";} { gsub("\n", "\r"); print;}' > TARGETrootopen.com
2018-06-04 20:24:48

linux命令:iconv -f utf8 -t utf16 /path/to/file /path/to/file utf16 -t utf8 -f iconv iconv -f utf8 -t utf16 /path/to/filerootopen.com
linux命令:iconv -f utf8 -t utf16 /path/to/file /path/to/file utf16 -t utf8 -f iconv iconv -f utf8 -t utf16 /path/to/filerootopen.com
2018-06-11 01:19:48

linux命令:iconv -c -f utf-8 -t ascii file.txt file.txt ascii -t utf-8 -f -c iconv iconv -c -f utf-8 -t ascii file.txtrootopen.com
linux命令:iconv -c -f utf-8 -t ascii file.txt file.txt ascii -t utf-8 -f -c iconv iconv -c -f utf-8 -t ascii file.txtrootopen.com
2018-01-07 15:25:48

linux命令:iconv -f utf8 -t ascii//TRANSLIT <output-file> <output-file> ascii//TRANSLIT -t utf8 -f iconv iconv -f utf8 -t ascii//TRANSLIT <output-file>rootopen.com
linux命令:iconv -f utf8 -t ascii//TRANSLIT <output-file> <output-file> ascii//TRANSLIT -t utf8 -f iconv iconv -f utf8 -t ascii//TRANSLIT <output-file>rootopen.com
2018-02-23 23:09:48

linux命令:iconv -f $(file -bi filename.ext | sed -e 's/.*[ ]charset=//') -t utf8 filename.ext > filename.ext filename.ext > filename.ext utf8 -t ]charset=//') 's/.*[ -e sed | filename.ext -bi $(file -f iconv iconv -f $(file -bi filename.ext | sed -e 's/.*[ ]charset=//') -t utf8 filename.ext > filename.extrootopen.com
linux命令:iconv -f $(file -bi filename.ext | sed -e 's/.*[ ]charset=//') -t utf8 filename.ext > filename.ext filename.ext > filename.ext utf8 -t ]charset=//') 's/.*[ -e sed | filename.ext -bi $(file -f iconv iconv -f $(file -bi filename.ext | sed -e 's/.*[ ]charset=//') -t utf8 filename.ext > filename.extrootopen.com
2018-02-13 08:25:48

Linux的Bash命令行(A-Z排序)