zoukankan      html  css  js  c++  java
  • 批量更改文件编码格式 utf8到gb2312

    一.查看文件编码

      file name;//可以显现文件的编码格式(有的系统不可以)

    二.文件编码转换

      使用iconv转换,

      iconv -f encoding -t encoding inputfile;

    例如:iconv -f utf-8 -t gb2312 file1;

    用法:iconv [选项..]  [文件]

    -f, 原始编码

    -t,输出编码

    信息:

    -l,---list 列举系统所有安装的已知字符集

    输出控制:

    -c 从输出中忽略无效的字符

    -o ---output  输出文件

    实例:遍历目录下的所有文件,改变utf8编码到gb2312

    for i in `find ./ -type f -name '*.txt'`;

    do

    echo $i

    echo ${i}.tmp

    iconv -f utf-8 -t gb2312 $i>${i}.tmp

    mv ${i}.tmp $i;

    done

    问题记录:

    iconv转换失败,脚本主机没有暗转gb2312编码,只有gbk,导致一开始转化老是失败。

    转载1:

    1. #!/bin/bash  
    2.   
    3. #1.变量定义  
    4. directory="/home/wzy/Downloads/execl"  
    5. f_encoding="utf-8"  
    6. t_encoding="gbk"  
    7.   
    8. #2.遍历子目录  
    9. for dir in `ls $directory`  
    10. do  
    11.     if [ -d $directory/$dir ]  
    12.     then  
    13.         #3.遍历子目录的文件  
    14.         for file in `ls $directory/$dir`  
    15.         do  
    16.             if [ -e $directory/$dir/$file ]  
    17.             then  
    18.                 #4.文件类型转换  
    19.                 iconv -f $f_encoding -t $t_encoding $directory/$dir/$file -o $directory/$dir/iconv.$file  
    20.                 #5.删除原始文件  
    21.                 if [ $? -eq 0 ]  
    22.                 then  
    23.                     rm $directory/$dir/$file  
    24.                 fi  
    25.             fi  
    26.         done  
    27.     fi  
    28. done 
  • 相关阅读:
    HDU 2095 find your present (2) (异或)
    UESTC 486 Good Morning (水题+坑!)
    UVa 111 History Grading (简单DP,LIS或LCS)
    UVa 11292 Dragon of Loowater (水题,排序)
    HDU 1503 Advanced Fruits (LCS+DP+递归)
    UVa 10881 Piotr's Ants (等价变换)
    UVa 11178 Morley's Theorem (几何问题)
    HDU 1285 确定比赛名次(拓扑排序)
    .net Core的例子
    TCP与UDP的区别
  • 原文地址:https://www.cnblogs.com/cyblogs/p/6091320.html
Copyright © 2011-2022 走看看