文件批量转码Shell脚本实现(这里以gbk18030转utf8为例)

(亲测可用)

#!/bin/bash
#print the derectory and file 

#待转码的文件所在的目录
for file in /data/yulong/qa/*
do
if [ -d "$file" ]
then
  echo "$file is directory"
elif [ -f "$file" ]
then
  file1=`basename $file`
  #转码后文件存储的目录
  dir=/data/yulong/qa_utf8/
  file2=$dir${file1%%.*}_utf8.csv
  iconv -f gb18030 -t utf8  $file > $file2
  echo "$file --gb18030--->--utf8-------> $file2"
fi
done

测试结果如下(显示转码成功):
这里写图片描述

更完善点如下(加了转码错误判断,没有测试,可能有问题):

#!/bin/bash
#print the derectory and file 
#此处目录为待转码的目录
for file in /data/yulong/qa/*
do
if [ -d "$file" ]
then
  echo "$file is directory" 
elif [ -f "$file" ]
then
  file1=`basename $file`
  #转码后文件存储在目录
  dir=/data/yulong/qa_utf8/
  file2=$dir${file1%%.*}_utf8.csv
  echo "$file ----> $file2"
  iconv -f gb18030 -t utf8  $file > $file2
  if [ $? -eq 1 ]
     then 
        echo "$file 转码失败"
        continue
  fi
fi
done

参考文章:Linuxshell字符串截取与拼接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值