脚本处理大数据文件

在处理XX行的生产问题时,碰到过一次一个几十个G的文件,其中有几万条数据有一些问题,应急版本又来不及下发。最后采用了脚本处理的方法,主要用cut及paste命令,主要就是快。用时大概几分钟吧。
下面详细介绍脚本的处理过程。
处理的核心是利用sed将问题文件中的错误数据替换为正确的数据
1.由于文件中内容过多,为了防止误操作,首先将文件按列切分为多个文件(问题文件内容每行定长)。
cutfile.sh

#! /bin/ksh

if [ $# != 1 ]
then
    echo "FORMAT: cutfile.sh inputfile !"
    exit 1;
fi

filename=$1

head -n 1  $filename > .filehead.txt
tail -n +2 $filename > .filebody.txt

cut -c1-44     .filebody.txt  > .fileblock_1
cut -c45-52    .filebody.txt  > fileblock_2_gmt_date.txt        # gmt_date
cut -c53-68    .filebody.txt  > .fileblock_3
cut -c69-76    .filebody.txt  > fileblock_4_local_date.txt      # local_date
cut -c77-82    .filebody.txt  > .fileblock_5
cut -c83-90    .filebody.txt  > fileblock_6_settlement_date.txt # settlement_date
cut -c91-98    .filebody.txt  > fileblock_7_capture_date.txt    # capture_date
cut -c99-294   .filebody.txt  > .fileblock_8
cut -c295-302  .filebody.txt  > fileblock_9_orig_date.txt       # orig_date
cut -c303-     .filebody.txt  > .fileblock_10

touch .CUT.OK

2.对切分开的错误文件做统计,查找出错次数
uniqfile.sh

#! /bin/ksh

if [ $# != 1 ]
then
    echo "FORMAT: nuiqfile.sh inputfile !"
    exit 1;
fi

filename=$1

cat  $filename | sort | uniq -c

3.对出错切分文件进行替换操作

#! /bin/ksh

if [ $# != 3 ]
then
    echo "FORMAT: sedfile.sh filename replacebefore replaceafter!"
    exit 1;
fi

if [ ! -f .CUT.OK ];then
    echo "please cut file,first!"
    exit 0
fi

filename=$1
repstr1=$2
repstr2=$3

sed "s/${repstr1}/${repstr2}/" $filename > .sedtmpfile

rm -f $filename
mv .sedtmpfile $filename

4.对处理过的文件进行合并,最后生成正确的文件

#! /bin/ksh

if [ $# != 1 ]
then
    echo "FORMAT: pastefile.sh inputfile !"
    exit 1;
fi

if [ ! -f .CUT.OK ];then
    echo "please cut file,first!"
    exit 0
fi
filename=$1

# 如果文件中有\t,那么指定paste时的间隔符比如@,所以操作之前要用grep "	" filename | wc -l验证
#paste -d @ .fileblock_1                \
paste .fileblock_1                     \
      fileblock_2_gmt_date.txt         \
      .fileblock_3                     \
      fileblock_4_local_date.txt       \
      .fileblock_5                     \
      fileblock_6_settlement_date.txt  \
      fileblock_7_capture_date.txt     \
      .fileblock_8                     \
      fileblock_9_orig_date.txt        \
      .fileblock_10 > .filebodytmp.txt

#sed -e 's/@//g' .filebodytmp.txt >  .filenewbody.txt
sed -e 's/	//g' .filebodytmp.txt >  .filenewbody.txt

cat .filehead.txt .filenewbody.txt > ${filename}.new

rm -f .fileblock_1                     \
      fileblock_2_gmt_date.txt        \
      .fileblock_3                     \
      fileblock_4_local_date.txt      \
      .fileblock_5                     \
      fileblock_6_settlement_date.txt \
      fileblock_7_capture_date.txt    \
      .fileblock_8                     \
      fileblock_9_orig_date.txt       \
      .fileblock_10

rm -f .filehead.txt .filebody.txt .filenewbody.txt .filebodytmp.txt .CUT.OK

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值