统计符合条件的行数、查找最符合某个发生时间的log文件的脚本

#!/bin/bash

function help() {
cat <<EOF
  Function:   Display matched lines sum by grep all files whose filename meets a filename template
  Usage:      vvcount.sh regex [min=n] [max=n] template [range]
  Params:
- regex:      a regular expression string used by grep
- min=n:      a trigger option, whose default value is -999. If matched lines is less than this value, this filename will be displayed.
- max=n:      a trigger option, whose default value is 3.    If matched lines is more than this value, this filename will be displayed.
- template:   a filename template, whose format is like "/dir/xxx_N.zzz". This tool will grep all "/dir/xxx_*" files.
- range:      With it, the template will be merged into "/dir/xxx_range*". eg. Template "/foo/xxx_N.log" and range '9' will be "/foo/xxx_9*"
EOF
    echo ""
}

if [ $# -lt 1 ]; then
    help;
    exit 0;
fi

stmpl="logcat_1.log"
sbase=""
strig=2
sztmp=$1

# get key string
szkey=$(echo "$1" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:])
shift;

# get min trigger
if [ $# -lt 1 ]; then
    help;
    exit 0;
fi
sztmp=$(echo "$1"|LANG=C awk '{i=match($0,/min=\-*[0-9]+/); if(RLENGTH>0){printf("%s",substr($0,i+4,RLENGTH));}else{printf("");}}' )
szmin=$(echo "$sztmp" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:])
if [ "x$szmin" = "x" ]; then
    szmin="-999";
else
    shift;
fi

# get max trigger
if [ $# -lt 1 ]; then
    help;
    exit 0;
fi
sztmp=$(echo "$1"|LANG=C awk '{i=match($0,/max=[0-9]+/); if(RLENGTH>0){printf("%s",substr($0,i+4,RLENGTH));}else{printf("");}}' )
szmax=$(echo "$sztmp" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:])
if [ "x$szmax" = "x" ]; then
    szmax=3;
else
    shift;
fi

# get filename template
if [ $# -lt 1 ]; then
    help;
    exit 0;
else
    stmpl=$1;
    shift;
fi

# get filename begin number
if [ $# -gt 0 ]; then
    expr $1 "+" 10 &> /dev/null
    if [ $? -eq 0 ]; then
        sbase=$1
    fi
fi

sztmp=$(echo "$stmpl"|LANG=C awk '{i=match($0,/_[a-zA-Z_0-9]+\./); if(RLENGTH>0){ s=substr($0,i,RLENGTH); j=match(s,/[0-9]+/); n=((RLENGTH>1)?(RLENGTH-1):0); printf("%s",substr($0,1,i+n));}else{printf("  ");}}' )
ftmpl="$sztmp$sbase*"
echo "-----------------------------------------------------------------------------"
echo "template: $ftmpl"
echo "min-trig: $szmin"
echo "max-trig: $szmax"

echo "-----------------------------------------------------------------------------"
flist=$(ls $ftmpl)
for f in $flist; do
    fname=`echo "$f" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:]`
    #echo "$fname"
    grep -c $szkey $fname | awk -v szk=$szkey -v tl=$szmin -v th=$szmax -v f=$fname '{ cnt=$0; if(cnt>=th || cnt<=tl){ printf("%s,\t total: %d\n",f,cnt);}}'
done

echo "-----------------------------------------------------------------------------"
echo "Finished!"
echo ""



//



#!/bin/bash

function help() {
cat <<EOF
  Function:   Display the files whose head 5 lines' logcat time is nearest with the 1st paramemeters which is a time string
  Usage:      vvnearby.sh timestring [threshold=n] template [range]
  Params:
- timestring: a ascii time string. eg. "Jan 18, 2012 8:35:31 PM"
- threshold:  seconds threshold to display filename
- template:   a filename template, whose format is like "/dir/xxx_N.zzz". This tool will grep all "/dir/xxx_*" files.
- range:      With it, the template will be merged into "/dir/xxx_range*". eg. Template "/foo/xxx_N.log" and range '9' will be "/foo/xxx_9*"
EOF
    echo ""
}

# get time string
if [ $# -lt 1 ]; then
    help;
    exit 0;
fi
sdate=$(echo "$1" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:])
idate=$(date +%s -d "$sdate")
sdate=$(date +%F\ %T -d@"$idate")
shift;

stmpl="logcat_1.log"
sbase=""
strig=180     # default threshold is 180 seconds

# get threshold
if [ $# -gt 0 ]; then
    sztmp=$(echo "$1"|LANG=C awk '{i=match($0,/threshold=[0-9]+/); if(RLENGTH>0){printf("%s",substr($0,i+10,RLENGTH));}else{printf("");}}' )
    strig=$(echo "$sztmp" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:])
    if [ "x$strig" = "x" ]; then
        strig=180;
    else
        shift;
    fi
fi

# get template
if [ $# -gt 0 ]; then
    stmpl=$1;
    shift;
fi

# get range
if [ $# -gt 0 ]; then
    expr $1 "+" 10 &> /dev/null
    if [ $? -eq 0 ]; then
        sbase=$1
        shift;
    fi
fi

sztmp=$(echo "$stmpl"|LANG=C awk '{i=match($0,/_[a-zA-Z_0-9]+\./); if(RLENGTH>0){ s=substr($0,i,RLENGTH); j=match(s,/[0-9]+/); n=((RLENGTH>1)?(RLENGTH-1):0); printf("%s",substr($0,1,i+n));}else{printf("  ");}}' )
ftmpl="$sztmp$sbase*"
echo "----------------------------------------------------"
echo "$ftmpl"

echo "----------------------------------------------------"
flist=$(ls $ftmpl)
for f in $flist; do
    fname=`echo "$f" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:]`
    #echo "$fname"
    #sztmp=$(head -5 $fname | awk 'BEGIN{jdate="";} {i=match($0,/\[\ +[0-9\.]+\]/); if(i>0&&RLENGTH>0){jdate=substr($0,1,RSTART-1);} } END{printf("%s",jdate);}')
    sztmp=$(head -5 $fname | awk 'BEGIN{jdate="";} {i=match($0,/\[\ +[0-9\.]+\]/); if(i>0&&RLENGTH>0){jdate=substr($0,1,RSTART-6);} } END{printf("%s",jdate);}')
    kdate=`echo "$sztmp" | sed -e 's/\(^ *\)//' -e 's/\( *$\)//' | tr -d [:cntrl:]`
    #echo "kdate=$kdate"
    jdate=$(date +%s -d "2012-$kdate")
    echo "$fname   (2012-$kdate  -  $sdate)" | awk -v f=$fname -v i=$idate -v j=$jdate -v trig=$strig '{n=j-i; if(i>j){k=i-j;}else{k=j-i;} if(k<trig){printf("%s,  secs=%d\n", $0, n); } }'
done

echo "----------------------------------------------------"
echo "Finished!"
echo ""









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值