Q:

How to select files based on timestamp of 15 minutes interval using Shell Script

I am trying to grep some data from log files (based on timestamp). I need to pick files from 15 minutes of interval, 4 times in each hour. I am able to do that for 10 minutes but for 15 minutes I my logic failed everytime.
This is my first shell script so there must be some issues. Please guide me.
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

A:

I'm assuming your shell is bash. I'm going to use bash's extended globbing to capture a list of files in the range of minutes, and store into an array
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 ... )

Be consistent with your variable names: either use lower case or upper. But don't use upper ;) -- one day you'll say PATH=something and your script will stop working.

