博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

文件逐行读取并进行相关处理的Unix Shell

Posted on 2009-02-26 22:43  小易的博客园  阅读(2221)  评论(0)    收藏  举报

  最近在外实习,学习Unix脚本,恰好测试环境需要,便小试牛刀,看看我写的的shell是不是很帅! 

###### Date : 2009.2.25 ###########
###### Author: landry yi ###########
###### CopyRight : Harbin Normal University@copyright2009 ###########
###### Email: yijiyong100@163.com ###########
mv ./bak/*.txt ./
REDODIR="/transfer/cmp_predeal/old/H01.20090101/redo"
OTHERDIR="/transfer/cmp_predeal/old/H01.20090101/other"
CMPTXT="/transfer/diff/14-cmp/bak/old_no_exist/0101H01/txt"
CMPTXTBAK="/transfer/diff/14-cmp/bak/old_no_exist/0101H01/txt/bak"

if [ -f ${CMPTXT}/*.txt ]
then
       find ${CMPTXT} -type f -name "*.txt" | while read file
       do 
           echo ${file}
           cat ${file} | awk  '{
                  msisdn=$1
                  phonefile=$10
                  filename="'$file'"
                  submsisdn=substr(msisdn,5)
                  if(phonefile == "")
                      printf("the phonefile is NULL,error ocurr line in [%d] file in [%s]\n",NR,filename);
                  else
                      printf("msisdn:[%s] phonefile[yveold.%s]\n",submsisdn,phonefile);
           }' $file

           length=`awk 'END {print NR}' $file`
           echo '######FILE [' $file '] BEGIN#######The line number of file:[' $file '] is [' $length ']\n:'
           i=1
           while  [ $i -le $length ]
           do
               PREX=yveold
               MSISDN=`awk '{if (NR == "'$i'") printf("%s\n",substr($1,5));}' $file`
               PHONEFILE=`awk '{if (NR == "'$i'") printf("%s\n",$NF);}' $file`
               if [ -z "$PHONEFILE" ]
               then
                   echo 'Phonefile NULL,error at file:['$file'] line:['$i'] msisdn:['$MSISDN']'>>error.list
               else
                   if [ -f ${REDODIR}/${PREX}.${PHONEFILE} ]
                    then
                       redoResult=`grep $MSISDN ${REDODIR}/${PREX}.${PHONEFILE} | head -1 | awk -F '/' '{print($4)'}`
                       if [ -n "$redoResult" ]
                       then
                            echo '######ERROR IN REDO######filename:[' $file ']#####msisdn:['$MSISDN']'>>redoResult.list
                            echo 'Error code :'$redoResult>>redoResult.list
                            echo '######---ERROR IN REDO END'>>redoResult.list
                       fi
                    else
                       echo 'phonefile['${PREX}.${PHONEFILE}']not at['$REDODIR']file:['$file']line:['$i']msisdn:['$MSISDN'['>>r.list
                    fi
                    if [ -f ${OTHERDIR}/${PREX}.${PHONEFILE} ]
                    then
                        otherResult=`grep $MSISDN ${OTHERDIR}/${PREX}.${PHONEFILE} | head -1 | awk -F '/' '{print($4)'}`
                        if [ -n "$otherResult" ]
                        then
                             echo '######ERROR IN OTHER######filename:[' $file ']#####msisdn:['$MSISDN']'>>otherResult.list
                             echo 'Error code :'$otherResult>>otherResult.list
                             echo '######---ERROR IN OTHER END'>>otherResult.list
                         fi
                    else
                      echo 'file['${PREX}.${PHONEFILE}']not at['$OTHERDIR']file:['$file']line:['$i']msisdn:['$MSISDN']'>>o.list
                    fi 
               fi              
               let i=i+1
            done
            echo '######FILE[' $file ']END######'
            let i=1
            mv  ${file} ${CMPTXTBAK}/
       done
fi