Chinaunix首页 | 论坛 | 博客
  • 博客访问: 561157
  • 博文数量: 287
  • 博客积分: 27
  • 博客等级: 民兵
  • 技术积分: 547
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-01 20:20
文章分类

全部博文(287)

文章存档

2015年(1)

2014年(95)

2013年(90)

2012年(101)

分类: 项目管理

2013-10-09 16:39:49

最近,借助sed+awk,实现了一个从结果文件中提取含有相应关键字的脚本。
    - 借助该脚本,可以方便获取关注的部分,并将相应部分保存于一个文件中,方便查阅
    - 初次实现的版本太按步就班,比较低效,因此,经过改进,实现了该版本,即使结果文件非常巨大,效果也非常理想
      -- 通过缩减范围,线性操作(详见注释)等方法提高效率,
      -- 基本做到了单次检索即可获取关键字索引
     
具体代码如下(鉴于隐私原因,有删减)

点击(此处)折叠或打开

  1. #! /bin/bash
  2. ################################################################################
  3. # Description: Fetch section from test result contains keywords (e.g. KO[123] | ABO) to facilitate test analysis.
  4. # ... omit ...
  5. ################################################################################
  6. _VERSION_=1.0

  7. TEST_DIR=
  8. RESULTS_DIR=
  9. R_PICK_DIR=

  10. #file name of *.check
  11. checkFile=

  12. #file name with picked info
  13. pickFile=

  14. #When KO[123] occurs, fetch line [n-20, n] and stores to pickFile
  15. nLookback=20

  16. curSslFile=._cur_SSL_File_
  17. ################################################################################
  18. # Init
  19. # - 接受多种参数输入方式,如-t/-t -d/-d,以方便使用
  20. ################################################################################
  21. init() {
  22.     if [[ "${TEST_DIR}" != "" ]]; then
  23.         RESULTS_DIR=${TEST_DIR}/results
  24.         cd "${RESULTS_DIR}" || {
  25.             echo "chdir to [${RESULTS_DIR}] failed"
  26.             exit 1
  27.         }
  28.         if [[ "${R_PICK_DIR}" == "" ]]; then
  29.             #获取当前目录下最新更改的并且名字前10个字符为数字的目录
  30.             R_PICK_DIR=`ls -Ft | egrep "[0-9]{10}.*/$" | awk -v RS= -F "\n" '{print $1}'`
  31.             if [ $? != 0 ]; then
  32.                 echo "failed to get direcotry contains latest test results"
  33.                 exit 1
  34.             fi
  35.         else
  36.             R_PICK_DIR=${R_PICK_DIR##*/}
  37.         fi
  38.         cd "${R_PICK_DIR}" || {
  39.             echo "failed chdir to [${RESULTS_DIR}/${R_PICK_DIR}"
  40.             exit 1
  41.         }        
  42.     else
  43.         if [[ "${R_PICK_DIR}" != "" ]]; then
  44.             cd "${R_PICK_DIR}" || {
  45.                 echo "failed chdir to [${R_PICK_DIR}]"
  46.                 exit 1
  47.             }
  48.             curDir=`pwd`
  49.             R_PICK_DIR=${curDir##*/}
  50.         else
  51.             usage;exit 1
  52.         fi
  53.     fi

  54.     if [ `ls *.check 2>/dev/null | wc -l` -ne 1 ];then
  55.         echo "`pwd` - *.check file not exists or extra check file found";exit 1
  56.     fi
  57.     
  58.     R_PICK_DIR=${R_PICK_DIR%/}
  59.     
  60.     checkFile=`ls *.check`
  61.     pickFile=${R_PICK_DIR}.pick    
  62. }

  63. ################################################################################
  64. # 1. Extract neccessary info from *.check
  65. # 2. Fetch Scriptname whoes Verdict is KO[123] | ABO
  66. # In: *.check
  67. # Out: *.pick
  68. ################################################################################
  69. retrieveVerdict() {
  70.     [[ -r "$1" ]] || {
  71.         echo ".check file not readable"; exit 1
  72.     }
  73.     cat /dev/null>"${pickFile}"
  74.     nStation=$(sed -n '/[.]station/=' $1)
  75.     nResults=$(sed -n '/[ ]*SCRIPT VERDICTS/=' $1)
  76.     if [ -z "$nStation" ] || [ -z "$nResults" ]; then
  77.         echo "Bad check format: `pwd`/$1">>"${pickFile}"; exit 1
  78.     fi
  79.     sed -n -e "1,${nStation}p" "$1" -n -e "${nResults},\$p" "$1" >> "${pickFile}"
  80.     
  81.     #通过sed缩小检索范围; awk中均为线性操作,单次执行即可获取目标数组。
  82.     eval $( sed -n "${nResults},\$p" ${checkFile} | awk
  83.     BEGIN{aSize=0;}
  84.     {if(($2 ~ /[0-9]+:[0-9]+:[0-9]+/) && ($5 ~ /KO[123]|ABO/)){
  85.         aArrF12[aSize]=$1":"$2;
  86.         aArrF3[aSize]=$3;
  87.         aArrF5[aSize]=$5;
  88.         aSize++;}
  89.     }
  90.     END { for (i=0;i
  91.         print "sArrF12["i"]="aArrF12[i];
  92.         print "sArrF3["i"]="aArrF3[i];
  93.         print "sArrF5["i"]="aArrF5[i];
  94.         }
  95.     }')
  96.     for ((i=0; i<${#sArrF12[@]}; i++))
  97.     do
  98.         case ${sArrF5[i]} in
  99.             KO[123]|ABO )
  100.                 retrieveSSL `echo "${sArrF12[i]}" | awk -F ":" '{printf("%02d%02d%02d%02d",$1,$2,$3,$4)}'`"-${sArrF3[i]}" "${sArrF5[i]}";;
  101.             *) ;;
  102.         esac
  103.     done
  104.     # Not a must to unset sArrF12/3/5 here
  105.     unset sArrF12
  106.     unset sArrF3
  107.     unset sArrF5
  108. }
  109. ################################################################################
  110. # Description: Process each ssl or ssl.gz
  111. # In: .
  112. # Out: *.pick
  113. ################################################################################
  114. retrieveSSL() {
  115.     sslName=$1.ssl*
  116.     if [ `ls *$sslName 2>/dev/null | wc -l` -ne 1 ];then
  117.         echo "`pwd` - ${sslName} not exists or extra file found"; return 1
  118.     fi
  119.     sslName=`ls *$sslName`
  120.         echo "################################################">>${pickFile}
  121.         echo "# ${sslName} $2">>${pickFile}
  122.         echo "################################################">>${pickFile}
  123.     less "${sslName}">"${curSslFile}" || {
  124.         echo "Error occurrs during dump sslFile - ${sslName}"; return 1
  125.     }
  126.     #通过分类,KO[123]及ABO,可缩减KO[123]正则匹配的范围,该情况下无需检索REASON:
  127.     case "$2" in
  128.         KO[123])
  129.             eval $(awk 'BEGIN{aSize=0;}
  130.             {    if($0 ~ /KO[123]|STS RESULT/) #单次检索
  131.                 {
  132.                     if($0 ~ /[#].*KO[123]/){}  #通过判断获取目标索引
  133.                     else if($0 ~/STS RESULT/) {print "lnStsResult="NR}
  134.                     else {print "lnKO["aSize++"]="NR}
  135.                 }
  136.             }' "${curSslFile}");;
  137.         ABO)
  138.             eval $(awk 'BEGIN{aSize=0;}
  139.             { if($0 ~ /KO[123]|STS RESULT|REASON:/) #单次检索
  140.               {
  141.                     if($0 ~ /[#].*KO[123]/){}       #通过判断获取目标索引
  142.                     else if($0 ~/STS RESULT/){print "lnStsResult="NR}
  143.                     else {print "lnKO["aSize++"]="NR}
  144.               }
  145.             }' "${curSslFile}");;
  146.         *)    echo "Undefined Keywords - $2"; return 1;;  
  147.     esac    
  148.     #remove elements behind of STS RESULTS
  149.     if [ -n "$lnStsResult" ]; then
  150.         for (( j=${#lnKO[@]}-1; j>=0; j-- ))
  151.         do
  152.             if [ ${lnKO[j]} -ge ${lnStsResult} ];then
  153.                 unset lnKO[j]
  154.             else
  155.                 break
  156.             fi
  157.         done
  158.     fi
  159.     #Retrieve KO[123] lines from ssl, and filled into .pick file
  160.     lnUp=0
  161.     for (( k=0; k<${#lnKO[@]}; k++ ))  #此循环为线性操作
  162.     do
  163.         lnDown=${lnKO[k]}
  164.         if [[ $(($lnDown-lnUp-1)) -lt ${nLookback} ]]; then
  165.             lnUpN=$(($lnUp+1))
  166.         else
  167.             lnUpN=$(($lnDown - $nLookback))
  168.         fi
  169.         sed -n "${lnUpN},${lnDown}p" "${curSslFile}" >> "${pickFile}"
  170.         lnUp=${lnKO[k]}
  171.     done
  172.     #Must: the former lnKO() would cause impact on newer ones
  173.     unset lnKO
  174.     
  175.     #Retrieve lines after STS RESULT, and filled into .pick file
  176.     if [ -n "$lnStsResult" ]; then
  177.         sed -n "${lnStsResult},\$p" "${curSslFile}" >> "${pickFile}"
  178.     fi
  179. }
  180. ################################################################################
  181. # Description: - Copy *.pick to destination
  182. # - Remove curSslFile
  183. ################################################################################
  184. endPick() {
  185.     if [[ ! -r $1 ]]; then
  186.         echo "$1 not readable in `pwd`"; exit 1
  187.     fi
  188.     dest=
  189.     if [[ ${TEST_STATION} != "" ]]; then
  190.         dest=/dest/${TEST_STATION}/Logs
  191.     else
  192.         dest=/dest/Logs
  193.     fi
  194.     if [[ ! -e "${dest}" ]]; then
  195.         echo "[${dest}] not exist"; exit 1
  196.     fi
  197.     
  198.     cp "$1" "${dest}/$1" || {
  199.         echo "Error occurs during copy: [$1] to [${dest}]"; exit 1
  200.     }
  201.     echo "# $1 copied to ${dest} successfully."
  202.     
  203.     rm "${curSslFile}" || {
  204.         echo "Error occurrs during remove sslFile - ${curSslFile}"; exit 1
  205.     }
  206. }

  207. ################################################################################
  208. # Usage
  209. ################################################################################
  210. usage(){
  211.     echo "Usage: `basename $0` [-t testDir] [-d resultDir] [-n nLookback] [-v] [-h]";
  212.     echo " -t Directory of testDir, e.g. /home/prop/TEST_DIR"
  213.     echo " -d Directory of resultDir, e.g. 0904233817-SYSTEM_RESET"
  214.     echo " -n Lookback n lines when KO1/2/3 | ABO found in .ssl file. Default value: 20"
  215.     echo " -v print version"
  216.     echo " -h print help"
  217. }

  218. ################################################################################
  219. # Main section
  220. # Procedure:
  221. # 1. Find or specify the test result directory
  222. # 2. Analyze the *.check file and fetch scripts name with check value - KO[123]|ABO
  223. # 3. Analyze each KO fastscript and fetch the section contains the KO cause
  224. # In: -t Full path of testDir, e.g. /home/prop/TEST_DIR, -d test_result_dir, -n display number of Lookback
  225. # Out: *.pick contains neccessary test results information
  226. ################################################################################
  227. if [ $# -ge 1 ]
  228. then
  229. OPTIND=1
  230. while getopts t:d:n:vh opt
  231. do
  232.     case $opt in
  233.         t)    TEST_DIR=${OPTARG%/};;
  234.         d)
  235.             R_PICK_DIR=${OPTARG%/};;
  236.         n)
  237.             if [[ $OPTARG == `echo "$OPTARG" | egrep '^[0-9]+$'` ]]
  238.             then
  239.                 nLookback=$OPTARG
  240.             else
  241.                 echo "bad format of nLookback, default value $nLookback used";
  242.             fi;;

  243.         v)    echo "Current version of $0 is: $_VERSION_";exit 0;;
  244.         h)    usage; exit 0;;
  245.         ?)    echo "Bad arguments"; exit 1 ;;
  246.     esac
  247. done
  248. OPTIND=1
  249. else
  250.     usage; exit 0
  251. fi
  252. #invoke internal function
  253. init
  254. retrieveVerdict ${checkFile}
  255. endPick ${pickFile}


阅读(2236) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~