shell中使用awk判斷文件中字段的值並進行替換的方法

stime=`date +%s`
#獲取當前時間
echo  開始處理文件:  B08101007_${month_id}.txt.gz
#處理文件
zcat B08101007.txt.gz | awk -F '\x01' '{
#壓縮文件查看方式使用zcat,分隔符爲'\X01'
     if($2 == 822) {n2="V0511300"}
#判斷第二個字段值是否是822,如果是替換成V0511300
     else if($2 == 833) {n2="V0511100"}
    else if($2 == 811) {n2="V0513100"}
    else if($2 == 832) {n2="V0511000"}
    else if($2 == 818) {n2="V0510300"}
    else {n2=""}
#其他輸出爲空
};{print $1"\x01"n2"\x01"$3"\x01"$4"\x01"$5"\x01"$6"\x01"$7"\x01"$8"\x01"$9"\x01"$10"\x01"$11"\x01"$12"\x01"$13"\x01"$14"\x01"$15}'>/mnt/sd03/data/sichuan/trans_area/B08101007.txt.new
#根據獲取到的值重新輸出到新文件
        etime=`date +%s`
s=`echo "scale=0; ($etime - $stime)%60" | bc`
m=`echo "scale=0; ($etime - $stime)/60%60" | bc`
h=`echo "scale=0; ($etime - $stime)/60/60" | bc`

echo  處理文件結束:  B08101007.txt.gz
echo `date +"%F %T"` end 耗時 $h 小時 $m 分鐘 $s 秒


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章