最近,借助sed+awk,实现了一个从结果文件中提取含有相应关键字的脚本。
- 借助该脚本,可以方便获取关注的部分,并将相应部分保存于一个文件中,方便查阅
- 初次实现的版本太按步就班,比较低效,因此,经过改进,实现了该版本,
即使结果文件非常巨大,效果也非常理想:
-- 通过缩减范围,线性操作(详见注释)等方法提高效率,
--
基本做到了单次检索即可获取关键字索引
具体代码如下(鉴于隐私原因,有删减)
-
#! /bin/bash
-
################################################################################
-
# Description: Fetch section from test result contains keywords (e.g. KO[123] | ABO) to facilitate test analysis.
-
# ... omit ...
-
################################################################################
-
_VERSION_=1.0
-
-
TEST_DIR=
-
RESULTS_DIR=
-
R_PICK_DIR=
-
-
#file name of *.check
-
checkFile=
-
-
#file name with picked info
-
pickFile=
-
-
#When KO[123] occurs, fetch line [n-20, n] and stores to pickFile
-
nLookback=20
-
-
curSslFile=._cur_SSL_File_
-
################################################################################
-
# Init
-
# - 接受多种参数输入方式,如-t/-t -d/-d,以方便使用
-
################################################################################
-
init() {
-
if [[ "${TEST_DIR}" != "" ]]; then
-
RESULTS_DIR=${TEST_DIR}/results
-
cd "${RESULTS_DIR}" || {
-
echo "chdir to [${RESULTS_DIR}] failed"
-
exit 1
-
}
-
if [[ "${R_PICK_DIR}" == "" ]]; then
-
#获取当前目录下最新更改的并且名字前10个字符为数字的目录
-
R_PICK_DIR=`ls -Ft | egrep "[0-9]{10}.*/$" | awk -v RS= -F "\n" '{print $1}'`
-
if [ $? != 0 ]; then
-
echo "failed to get direcotry contains latest test results"
-
exit 1
-
fi
-
else
-
R_PICK_DIR=${R_PICK_DIR##*/}
-
fi
-
cd "${R_PICK_DIR}" || {
-
echo "failed chdir to [${RESULTS_DIR}/${R_PICK_DIR}"
-
exit 1
-
}
-
else
-
if [[ "${R_PICK_DIR}" != "" ]]; then
-
cd "${R_PICK_DIR}" || {
-
echo "failed chdir to [${R_PICK_DIR}]"
-
exit 1
-
}
-
curDir=`pwd`
-
R_PICK_DIR=${curDir##*/}
-
else
-
usage;exit 1
-
fi
-
fi
-
-
if [ `ls *.check 2>/dev/null | wc -l` -ne 1 ];then
-
echo "`pwd` - *.check file not exists or extra check file found";exit 1
-
fi
-
-
R_PICK_DIR=${R_PICK_DIR%/}
-
-
checkFile=`ls *.check`
-
pickFile=${R_PICK_DIR}.pick
-
}
-
-
################################################################################
-
# 1. Extract neccessary info from *.check
-
# 2. Fetch Scriptname whoes Verdict is KO[123] | ABO
-
# In: *.check
-
# Out: *.pick
-
################################################################################
-
retrieveVerdict() {
-
[[ -r "$1" ]] || {
-
echo ".check file not readable"; exit 1
-
}
-
cat /dev/null>"${pickFile}"
-
nStation=$(sed -n '/[.]station/=' $1)
-
nResults=$(sed -n '/[ ]*SCRIPT VERDICTS/=' $1)
-
if [ -z "$nStation" ] || [ -z "$nResults" ]; then
-
echo "Bad check format: `pwd`/$1">>"${pickFile}"; exit 1
-
fi
-
sed -n -e "1,${nStation}p" "$1" -n -e "${nResults},\$p" "$1" >> "${pickFile}"
-
-
#通过sed缩小检索范围; awk中均为线性操作,单次执行即可获取目标数组。
-
eval $( sed -n "${nResults},\$p" ${checkFile} | awk '
-
BEGIN{aSize=0;}
-
{if(($2 ~ /[0-9]+:[0-9]+:[0-9]+/) && ($5 ~ /KO[123]|ABO/)){
-
aArrF12[aSize]=$1":"$2;
-
aArrF3[aSize]=$3;
-
aArrF5[aSize]=$5;
-
aSize++;}
-
}
-
END { for (i=0;i
-
print "sArrF12["i"]="aArrF12[i];
-
print "sArrF3["i"]="aArrF3[i];
-
print "sArrF5["i"]="aArrF5[i];
-
}
-
}')
-
for ((i=0; i<${#sArrF12[@]}; i++))
-
do
-
case ${sArrF5[i]} in
-
KO[123]|ABO )
-
retrieveSSL `echo "${sArrF12[i]}" | awk -F ":" '{printf("%02d%02d%02d%02d",$1,$2,$3,$4)}'`"-${sArrF3[i]}" "${sArrF5[i]}";;
-
*) ;;
-
esac
-
done
-
# Not a must to unset sArrF12/3/5 here
-
unset sArrF12
-
unset sArrF3
-
unset sArrF5
-
}
-
################################################################################
-
# Description: Process each ssl or ssl.gz
-
# In: .
-
# Out: *.pick
-
################################################################################
-
retrieveSSL() {
-
sslName=$1.ssl*
-
if [ `ls *$sslName 2>/dev/null | wc -l` -ne 1 ];then
-
echo "`pwd` - ${sslName} not exists or extra file found"; return 1
-
fi
-
sslName=`ls *$sslName`
-
echo "################################################">>${pickFile}
-
echo "# ${sslName} $2">>${pickFile}
-
echo "################################################">>${pickFile}
-
less "${sslName}">"${curSslFile}" || {
-
echo "Error occurrs during dump sslFile - ${sslName}"; return 1
-
}
-
#通过分类,KO[123]及ABO,可缩减KO[123]正则匹配的范围,该情况下无需检索REASON:
-
case "$2" in
-
KO[123])
-
eval $(awk 'BEGIN{aSize=0;}
-
{ if($0 ~ /KO[123]|STS RESULT/) #单次检索
-
{
-
if($0 ~ /[#].*KO[123]/){} #通过判断获取目标索引
-
else if($0 ~/STS RESULT/) {print "lnStsResult="NR}
-
else {print "lnKO["aSize++"]="NR}
-
}
-
}' "${curSslFile}");;
-
ABO)
-
eval $(awk 'BEGIN{aSize=0;}
-
{ if($0 ~ /KO[123]|STS RESULT|REASON:/) #单次检索
-
{
-
if($0 ~ /[#].*KO[123]/){} #通过判断获取目标索引
-
else if($0 ~/STS RESULT/){print "lnStsResult="NR}
-
else {print "lnKO["aSize++"]="NR}
-
}
-
}' "${curSslFile}");;
-
*) echo "Undefined Keywords - $2"; return 1;;
-
esac
-
#remove elements behind of STS RESULTS
-
if [ -n "$lnStsResult" ]; then
-
for (( j=${#lnKO[@]}-1; j>=0; j-- ))
-
do
-
if [ ${lnKO[j]} -ge ${lnStsResult} ];then
-
unset lnKO[j]
-
else
-
break
-
fi
-
done
-
fi
-
#Retrieve KO[123] lines from ssl, and filled into .pick file
-
lnUp=0
-
for (( k=0; k<${#lnKO[@]}; k++ )) #此循环为线性操作
-
do
-
lnDown=${lnKO[k]}
-
if [[ $(($lnDown-lnUp-1)) -lt ${nLookback} ]]; then
-
lnUpN=$(($lnUp+1))
-
else
-
lnUpN=$(($lnDown - $nLookback))
-
fi
-
sed -n "${lnUpN},${lnDown}p" "${curSslFile}" >> "${pickFile}"
-
lnUp=${lnKO[k]}
-
done
-
#Must: the former lnKO() would cause impact on newer ones
-
unset lnKO
-
-
#Retrieve lines after STS RESULT, and filled into .pick file
-
if [ -n "$lnStsResult" ]; then
-
sed -n "${lnStsResult},\$p" "${curSslFile}" >> "${pickFile}"
-
fi
-
}
-
################################################################################
-
# Description: - Copy *.pick to destination
-
# - Remove curSslFile
-
################################################################################
-
endPick() {
-
if [[ ! -r $1 ]]; then
-
echo "$1 not readable in `pwd`"; exit 1
-
fi
-
dest=
-
if [[ ${TEST_STATION} != "" ]]; then
-
dest=/dest/${TEST_STATION}/Logs
-
else
-
dest=/dest/Logs
-
fi
-
if [[ ! -e "${dest}" ]]; then
-
echo "[${dest}] not exist"; exit 1
-
fi
-
-
cp "$1" "${dest}/$1" || {
-
echo "Error occurs during copy: [$1] to [${dest}]"; exit 1
-
}
-
echo "# $1 copied to ${dest} successfully."
-
-
rm "${curSslFile}" || {
-
echo "Error occurrs during remove sslFile - ${curSslFile}"; exit 1
-
}
-
}
-
-
################################################################################
-
# Usage
-
################################################################################
-
usage(){
-
echo "Usage: `basename $0` [-t testDir] [-d resultDir] [-n nLookback] [-v] [-h]";
-
echo " -t Directory of testDir, e.g. /home/prop/TEST_DIR"
-
echo " -d Directory of resultDir, e.g. 0904233817-SYSTEM_RESET"
-
echo " -n Lookback n lines when KO1/2/3 | ABO found in .ssl file. Default value: 20"
-
echo " -v print version"
-
echo " -h print help"
-
}
-
-
################################################################################
-
# Main section
-
# Procedure:
-
# 1. Find or specify the test result directory
-
# 2. Analyze the *.check file and fetch scripts name with check value - KO[123]|ABO
-
# 3. Analyze each KO fastscript and fetch the section contains the KO cause
-
# In: -t Full path of testDir, e.g. /home/prop/TEST_DIR, -d test_result_dir, -n display number of Lookback
-
# Out: *.pick contains neccessary test results information
-
################################################################################
-
if [ $# -ge 1 ]
-
then
-
OPTIND=1
-
while getopts t:d:n:vh opt
-
do
-
case $opt in
-
t) TEST_DIR=${OPTARG%/};;
-
d)
-
R_PICK_DIR=${OPTARG%/};;
-
n)
-
if [[ $OPTARG == `echo "$OPTARG" | egrep '^[0-9]+$'` ]]
-
then
-
nLookback=$OPTARG
-
else
-
echo "bad format of nLookback, default value $nLookback used";
-
fi;;
-
-
v) echo "Current version of $0 is: $_VERSION_";exit 0;;
-
h) usage; exit 0;;
-
?) echo "Bad arguments"; exit 1 ;;
-
esac
-
done
-
OPTIND=1
-
else
-
usage; exit 0
-
fi
-
#invoke internal function
-
init
-
retrieveVerdict ${checkFile}
-
endPick ${pickFile}
阅读(2236) | 评论(0) | 转发(0) |