文件逐行讀取並進行相關處理的Unix Shell 腳本

  最近在外實習,學習Unix腳本,恰好測試環境需要,便小試牛刀,看看我寫的的shell是不是很帥!

 

###### Date : 2009.2.25 ###########
###### Author: landry yi ###########
###### CopyRight : Harbin Normal University@copyright2009 ###########
###### Email: [email protected] ###########
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

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