linux 下的文件编码格式转换

使用场景:

在 linux 平台上对文件格式进行转换,比如将GBK格式的文件转换为UTF8格式

转换方法

1. 使用 iconv

使用 man 查看 iconv 的帮助命令,比较简单

NAME
       iconv - Convert encoding of given files from one encoding to another
SYNOPSIS
       iconv -f encoding -t encoding inputfile
DESCRIPTION
       The iconv program converts the encoding of characters in inputfile
       from one coded character set to another. The result is written to
       standard output unless otherwise specified by the --output option.
       --from-code, -f encoding
       Convert characters from encoding
       --to-code, -t encoding
       Convert characters to encoding
       --list
       List known coded character sets
       --output, -o file
       Specify output file (instead of stdout)
       --verbose
       Print progress information.

下面将gbk格式的文件转换utf8格式,命令为:

iconv -f gbk -t utf8 oldFile > newFile

2. 使用 enca

enca相比于iconv的优势在于它可以获得文件的编码格式,这样就可以在不必知道文件格式的情况下将文件转换为想要的格式
enca不是系统自带的,需要安装,安装过程如下:

#下载&解压
wget http://dl.cihar.com/enca/enca-1.13.tar.gz #可以自己去找最新版
tar -zxvf enca-1.13.tar.gz
cd enca-1.13
#编译安装
./configure
make
make install

enca的用法

enca -L zh_CN file    #查看file的编码格式
enca -L zh_CN -x UTF-8 file   #将file转换成utf8的编码格式,覆盖原来的文件
enca -L zh_CN -x UTF-8 < file1 > file2   #转换之后存成file2文件,不覆盖原来的文件

目前对enca的使用比较有限,之后再进行补充

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值