awk sample

#!/bin/bash

start='2012-07-25 15:45'

end='2012-07-25 22:45'

reportName="responseTimeReport"

reportNameByMin="responseTimeReportByMin"

hour_S=`echo ${start:11:2} | sed s/^0//`

hour_E=`echo ${end:11:2} | sed s/^0//`

awk -v start="$start" -v end="$end" -v reportNameByMin="$reportNameByMin" -F, '

    $2>=start && $2<=end

    {

        arr_num[$3"@"substr($2,1,16)"@"$4"@"$5]++;

        arr_rsp[$3"@"substr($2,1,16)"@"$4"@"$5]+=$6;

    }

    END{

        for(key in arr_num){arr_sort[i++]=key};

        len=asort(arr_sort);

        for(i=1;i<=len;i++){key=arr_sort[i]; split(key, arr_key, "@");

        if(arr_key[4]=="TRADING_ELIGIBILITY_CHECKING"){arr_key[4]="TRADING_ELI_CHEC"}else{};

        printf("%s,%s,%s,%s,%d,%.2f\n", arr_key[1],arr_key[2],arr_key[3],arr_key[4],arr_num[key],arr_rsp[key]/arr_num[key]) > reportNameByMin};

    }' debug.log

 

echo -e "##Overall Result##" > $reportName

awk -v reportName="$reportName" -F, '{arr_num[$3"@"$4]+=$5; arr_rsp[$3"@"$4]+=($5*$6);total+=$5}END{printf("Total volume: "total"\n\n") >> reportName; printf("%s\t%-20s\t%s\t%s\n", "CHANNEL","FUN","VOLUME","AVG(ms)") >> reportName; printf("%s\t%-20s\t%s\t%s\n", "-------","--------------------","------","-------") >> reportName; for(key in arr_num){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i]; split(key, arr_key, "@"); printf("%-5s\t%-20s\t%6d\t%6.2f\t\n", arr_key[1],arr_key[2],arr_num[key],arr_rsp[key]/arr_num[key]) >> reportName};}' $reportNameByMin

echo -e "\n##Result by machine##" >> $reportName

awk -v reportName="$reportName" -F, '{arr_num[$1"@"$3"@"$4]+=$5; arr_rsp[$1"@"$3"@"$4]+=($5*$6);}END{printf("%-20s\t%s\t%-20s\t%s\t%s\n", "HOST","CHANNEL","FUN","VOLUME","AVG(ms)") >> reportName; printf("%-20s\t%s\t%-20s\t%s\t%s\n", "--------------------","-------","--------------------","------","-------") >> reportName; for(key in arr_num){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i]; split(key, arr_key, "@"); printf("%-20s\t%-5s\t%-20s\t%6d\t%6.2f\t\n", arr_key[1],arr_key[2],arr_key[3],arr_num[key],arr_rsp[key]/arr_num[key]) >> reportName};}' $reportNameByMin

if [ $((hour_E - hour_S)) == 1 ];

then

    echo -e "\n##Result by 15 minutes##" >> $reportName

    awk -v start="${start//[-:]/ } 00" -v end="${end//[-:]/ } 00" -v reportName="$reportName" -F, 'BEGIN{Time1=strftime("%Y-%m-%d %H:%M",mktime(start)+15*60);Time2=strftime("%Y-%m-%d %H:%M",mktime(start)+30*60);Time3=strftime("%Y-%m-%d %H:%M",mktime(start)+45*60);Time4=strftime("%Y-%m-%d %H:%M",mktime(start)+60*60);}{if($2<Time1){arr_num1[$3"@"$4]+=$5;arr_rsp1[$3"@"$4]+=($5*$6)}else if($2<Time2){arr_num2[$3"@"$4]+=$5;arr_rsp2[$3"@"$4]+=($5*$6)}else if($2<Time3){arr_num3[$3"@"$4]+=$5;arr_rsp3[$3"@"$4]+=($5*$6)}else if($2<Time4){arr_num4[$3"@"$4]+=$5;arr_rsp4[$3"@"$4]+=($5*$6)}else{};}END{printf("%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "CHANNEL","FUN","VOL15","AVG15","VOL30","AVG30","VOL45","AVG45","VOL60","AVG60") >> reportName; printf("%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n","-------","--------------------","------","-------","------","-------","------","-------","------","-------") >> reportName; for(key in arr_num1){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i]; split(key, arr_key, "@"); printf("%-5s\t%-20s\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\n", arr_key[1],arr_key[2],arr_num1[key],arr_rsp1[key]/(arr_num1[key]+1),arr_num2[key],arr_rsp2[key]/(arr_num2[key]+1),arr_num3[key],arr_rsp3[key]/(arr_num3[key]+1),arr_num4[key],arr_rsp4[key]/(arr_num4[key]+1)) >> reportName};}' $reportNameByMin

    echo -e "\n##Result by machine/15 minutes##" >> $reportName

    awk -v start="${start//[-:]/ } 00" -v end="${end//[-:]/ } 00" -v reportName="$reportName" -F, 'BEGIN{Time1=strftime("%Y-%m-%d %H:%M",mktime(start)+15*60);Time2=strftime("%Y-%m-%d %H:%M",mktime(start)+30*60);Time3=strftime("%Y-%m-%d %H:%M",mktime(start)+45*60);Time4=strftime("%Y-%m-%d %H:%M",mktime(start)+60*60);}{if($2<Time1){arr_num1[$1"@"$3"@"$4]+=$5;arr_rsp1[$1"@"$3"@"$4]+=($5*$6)}else if($2<Time2){arr_num2[$1"@"$3"@"$4]+=$5;arr_rsp2[$1"@"$3"@"$4]+=($5*$6)}else if($2<Time3){arr_num3[$1"@"$3"@"$4]+=$5;arr_rsp3[$1"@"$3"@"$4]+=($5*$6)}else if($2<Time4){arr_num4[$1"@"$3"@"$4]+=$5;arr_rsp4[$1"@"$3"@"$4]+=($5*$6)}else{};}END{printf("%-20s\t%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "HOST","CHANNEL","FUN","VOL15","AVG15","VOL30","AVG30","VOL45","AVG45","VOL60","AVG60") >> reportName; printf("%-20s\t%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "--------------------","-------","--------------------","------","-------","------","-------","------","-------","------","-------") >> reportName; for(key in arr_num1){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i]; split(key, arr_key, "@"); printf("%-20s\t%-5s\t%-20s\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\n", arr_key[1],arr_key[2],arr_key[3],arr_num1[key],arr_rsp1[key]/(arr_num1[key]+1),arr_num2[key],arr_rsp2[key]/(arr_num2[key]+1),arr_num3[key],arr_rsp3[key]/(arr_num3[key]+1),arr_num4[key],arr_rsp4[key]/(arr_num4[key]+1)) >> reportName};}' $reportNameByMin

    echo -e "\n##Volume by machine##" >> $reportName

    awk -v start="${start//[-:]/ } 00" -v end="${end//[-:]/ } 00" -v reportName="$reportName" -F, 'BEGIN{Time1=strftime("%Y-%m-%d %H:%M",mktime(start)+15*60);Time2=strftime("%Y-%m-%d %H:%M",mktime(start)+30*60);Time3=strftime("%Y-%m-%d %H:%M",mktime(start)+45*60);Time4=strftime("%Y-%m-%d %H:%M",mktime(start)+60*60);}{if($2<Time1){arr_num1[$1]+=$5;arr_rsp1[$1]+=($5*$6)}else if($2<Time2){arr_num2[$1]+=$5;arr_rsp2[$1]+=($5*$6)}else if($2<Time3){arr_num3[$1]+=$5;arr_rsp3[$1]+=($5*$6)}else if($2<Time4){arr_num4[$1]+=$5;arr_rsp4[$1]+=($5*$6)}else{};arr_numTotal[$1]+=$5}END{printf("%-20s\t%s\t%s\t%s\t%s\t%s\n", "HOST","VOL15","VOL30","VOL45","VOL60","TOTAL") >> reportName; printf("%-20s\t%s\t%s\t%s\t%s\t%s\n", "--------------------","-------","------","-------","------","-------") >> reportName; for(key in arr_num1){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i];printf("%-20s\t%6d\t%6d\t%6d\t%6d\t%6d\n",key,arr_num1[key],arr_num2[key],arr_num3[key],arr_num4[key],arr_numTotal[key]) >> reportName};}' $reportNameByMin

    echo -e "\n##Volume by minute##" >> $reportName

    awk -v reportName="$reportName" -F, '{arr_num[substr($2,1,16)]+=$5;}END{printf("%-20s\t%s\n", "MINUTE","VOLUME") >> reportName; printf("%-20s\t%s\n", "--------------------","------") >> reportName; for(key in arr_num){printf("%-20s\t%6d\n",key,arr_num[key]) >> reportName};}' $reportNameByMin

elif [ $((hour_E - hour_S)) == 8 ];

then

    echo -e "\n##Result by 2 hours##" >> $reportName

    awk -v start="${start//[-:]/ } 00" -v end="${end//[-:]/ } 00" -v reportName="$reportName" -F, 'BEGIN{Time1=strftime("%Y-%m-%d %H:%M",mktime(start)+2*60*60);Time2=strftime("%Y-%m-%d %H:%M",mktime(start)+4*60*60);Time3=strftime("%Y-%m-%d %H:%M",mktime(start)+6*60*60);Time4=strftime("%Y-%m-%d %H:%M",mktime(start)+8*60*60);}{if($2<Time1){arr_num1[$3"@"$4]+=$5;arr_rsp1[$3"@"$4]+=($5*$6)}else if($2<Time2){arr_num2[$3"@"$4]+=$5;arr_rsp2[$3"@"$4]+=($5*$6)}else if($2<Time3){arr_num3[$3"@"$4]+=$5;arr_rsp3[$3"@"$4]+=($5*$6)}else if($2<Time4){arr_num4[$3"@"$4]+=$5;arr_rsp4[$3"@"$4]+=($5*$6)}else{};}END{printf("%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "CHANNEL","FUN","VOL2","AVG2","VOL4","AVG4","VOL6","AVG6","VOL8","AVG8") >> reportName; printf("%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n","-------","--------------------","------","-------","------","-------","------","-------","------","-------") >> reportName; for(key in arr_num1){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i]; split(key, arr_key, "@"); printf("%-5s\t%-20s\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\n", arr_key[1],arr_key[2],arr_num1[key],arr_rsp1[key]/(arr_num1[key]+1),arr_num2[key],arr_rsp2[key]/(arr_num2[key]+1),arr_num3[key],arr_rsp3[key]/(arr_num3[key]+1),arr_num4[key],arr_rsp4[key]/(arr_num4[key]+1)) >> reportName};}' $reportNameByMin

    echo -e "\n##Result by machine/2 hours##" >> $reportName

    awk -v start="${start//[-:]/ } 00" -v end="${end//[-:]/ } 00" -v reportName="$reportName" -F, 'BEGIN{Time1=strftime("%Y-%m-%d %H:%M",mktime(start)+2*60*60);Time2=strftime("%Y-%m-%d %H:%M",mktime(start)+4*60*60);Time3=strftime("%Y-%m-%d %H:%M",mktime(start)+6*60*60);Time4=strftime("%Y-%m-%d %H:%M",mktime(start)+8*60*60);}{if($2<Time1){arr_num1[$1"@"$3"@"$4]+=$5;arr_rsp1[$1"@"$3"@"$4]+=($5*$6)}else if($2<Time2){arr_num2[$1"@"$3"@"$4]+=$5;arr_rsp2[$1"@"$3"@"$4]+=($5*$6)}else if($2<Time3){arr_num3[$1"@"$3"@"$4]+=$5;arr_rsp3[$1"@"$3"@"$4]+=($5*$6)}else if($2<Time4){arr_num4[$1"@"$3"@"$4]+=$5;arr_rsp4[$1"@"$3"@"$4]+=($5*$6)}else{};}END{printf("%-20s\t%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "HOST","CHANNEL","FUN","VOL2","AVG2","VOL4","AVG4","VOL6","AVG6","VOL8","AVG8") >> reportName; printf("%-20s\t%s\t%-20s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "--------------------","-------","--------------------","------","-------","------","-------","------","-------","------","-------") >> reportName; for(key in arr_num1){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i]; split(key, arr_key, "@"); printf("%-20s\t%-5s\t%-20s\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\t%6d\t%6.2f\n", arr_key[1],arr_key[2],arr_key[3],arr_num1[key],arr_rsp1[key]/(arr_num1[key]+1),arr_num2[key],arr_rsp2[key]/(arr_num2[key]+1),arr_num3[key],arr_rsp3[key]/(arr_num3[key]+1),arr_num4[key],arr_rsp4[key]/(arr_num4[key]+1)) >> reportName};}' $reportNameByMin

    echo -e "\n##Volume by machine##" >> $reportName

    awk -v start="${start//[-:]/ } 00" -v end="${end//[-:]/ } 00" -v reportName="$reportName" -F, 'BEGIN{Time1=strftime("%Y-%m-%d %H:%M",mktime(start)+2*60*60);Time2=strftime("%Y-%m-%d %H:%M",mktime(start)+4*60*60);Time3=strftime("%Y-%m-%d %H:%M",mktime(start)+6*60*60);Time4=strftime("%Y-%m-%d %H:%M",mktime(start)+8*60*60);}{if($2<Time1){arr_num1[$1]+=$5;arr_rsp1[$1]+=($5*$6)}else if($2<Time2){arr_num2[$1]+=$5;arr_rsp2[$1]+=($5*$6)}else if($2<Time3){arr_num3[$1]+=$5;arr_rsp3[$1]+=($5*$6)}else if($2<Time4){arr_num4[$1]+=$5;arr_rsp4[$1]+=($5*$6)}else{};arr_numTotal[$1]+=$5}END{printf("%-20s\t%s\t%s\t%s\t%s\t%s\n", "HOST","VOL2","VOL4","VOL6","VOL8","TOTAL") >> reportName; printf("%-20s\t%s\t%s\t%s\t%s\t%s\n", "--------------------","-------","------","-------","------","-------") >> reportName; for(key in arr_num1){arr_sort[i++]=key}; len=asort(arr_sort); for(i=1;i<=len;i++){key=arr_sort[i];printf("%-20s\t%6d\t%6d\t%6d\t%6d\t%6d\n",key,arr_num1[key],arr_num2[key],arr_num3[key],arr_num4[key],arr_numTotal[key]) >> reportName};}' $reportNameByMin

    echo -e "\n##Volume by minute##" >> $reportName

    awk -v reportName="$reportName" -F, '{arr_num[substr($2,1,16)]+=$5;}END{printf("%-20s\t%s\n", "MINUTE","VOLUME") >> reportName; printf("%-20s\t%s\n", "--------------------","------") >> reportName; for(key in arr_num){printf("%-20s\t%6d\n",key,arr_num[key]) >> reportName};}' $reportNameByMin

else

    echo -e "\n\nWrong start/end time!" >> $reportName

fi

cat $reportName

rm -f $reportNameByMin

 

猜你喜欢

转载自buralin.iteye.com/blog/1606197
awk