我正在尝试grep
来自日志文件的某些数据(基于timestamp
)。我需要从15分钟的间隔中选择文件,每小时4次。我能够做到10分钟,但是15分钟我的逻辑每次都失败了。
这是我的第一个shell script
因此必定存在一些问题。请指导我。
while [ ${HR} -lt 24 ]
do
MI=0
COUNT=1
if [ $HR -le 9 ]
then
Bhr=0$HR
else
Bhr=$HR
fi
while [ $COUNT -lt 4 ]
do
Bsp=$MI*
fname="TSG_${dt}_${Bhr}_${Bsp}.log.gz"
echo $fname
MINUTE=15
MINUTE=`expr $MINUTE \* $COUNT`
echo "processing on" Date ${dt} Hour $HR Minute ${MINUTE}
resp=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | wc -l`
rejections=`zgrep -i "tsgWorker for Dynamic authentication rejection" ${fname} 2>/dev/null | wc -l`
sec1=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:1-"| wc -l`
sec2=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:2-"| wc -l`
sec3=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:3-"| wc -l`
sec4=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:4-"| wc -l`
sec5=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:5-"| wc -l`
sec6=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:6-"| wc -l`
sec7=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:7-"| wc -l`
sec8=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:8-"| wc -l`
sec9=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:9-"| wc -l`
sec10=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:1.-"| wc -l`
sec20=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:2.-"| wc -l`
sec30=`zgrep "AUTHENTICATION_RESPONSE" ${fname} 2>/dev/null | zgrep "diffTime:3.-"| wc -l`
echo $ddt $Bhr $MINUTE, "Authentication", $sec1, $sec2, $sec3, $sec4, $sec5, $sec6, $sec7, $sec8, $sec9, $sec10, $sec20, $sec30, $rejections >> $OUTPUTFILE
MI=$(((MI/15)*15))
if [ $MI -le 15 ]
then
MI=`expr $MI + 1`
else
COUNT=`expr $COUNT + 1`
fi
done
HR=`expr $HR + 1`
done
答案 0 :(得分:1)
我假设你的shell是bash。我将使用bash的扩展globbing捕获几分钟范围内的文件列表,并存储到数组中
min=$( date +%_M ) # the underscore replaces leading 0 with space
shopt -s extglob
if (( 0 <= min && min < 15 )); then
files=( TSG_${dt}_${hr}_+(0[0-9]|1[0-4]).log.gz )
elif (( 15 <= min && min < 30 )); then
files=( TSG_${dt}_${hr}_+(1[5-9]|2[0-9]).log.gz )
elif (( 30 <= min && min < 45 )); then
files=( TSG_${dt}_${hr}_+(3[0-9]|4[0-4]).log.gz )
elif (( 45 <= min && min <= 59 )); then
files=( TSG_${dt}_${hr}_+(4[5-9]|5[0-9]).log.gz )
fi
for file in "${files[@]}"; do
: do something with "$file"
done
# or do something with all the files:
output=$( zcat "${files[@]}" | grep ... )
与变量名一致:使用小写或大写。但是不要使用鞋帮;) - 有一天你会说PATH=something
并且你的剧本将停止工作。