一个awk grep 日志分析统计脚本

发布时间:2019-12-17编辑:脚本学堂
分享一个awk命令进行日志分析与统计的脚本,在linux shell中awk命令与grep命令进行日志分析非常不错,有需要的朋友参考下。

linuxjishu/13830.html target=_blank class=infotextkey>awk grep日志分析统计脚本

统计参数:按日期统计
日期格式:统计数据用[]包含;每行数据中包含日期字符串;关键字区分特定的业务逻辑
 

复制代码 代码示例:

#!/bin/bash 
#

stat_time=$1 
 
if [ ! -n "$stat_time" ]; then 
 
  echo "please put a stat time !" 
  read time 
  stat_time=$time 
   
  if [ ! -n "$stat_time" ]; then 
     echo "you put stat_time is null! stat end !" 
     exit   
  fi 
 
fi 
 
echo "you put stat time is:$stat_time" 
 
stat_path="/home/java小生/tigermachine_stat" 
stat_file_path="" 
 
if [ ! -x "$stat_path" ];then 
 
   mkdir $stat_path ; 
fi 
 
stat_file_path=$stat_path/stat.log 
 
cat catalina.out |grep $stat_time | grep "changeTigerMachinePoker gameCost" > $stat_file_path 
cat catalina.out |grep $stat_time | grep "tiger machine spinWin pokerType" >> $stat_file_path 
  
 
changePokerTotalCost=`cat $stat_file_path |grep 'changeTigerMachinePoker gameCost'|awk -F[][] '{print $6}' |awk 'BEGIN{total=0}{totalCost+=$1}END{print totalCost}'` 
 
spinTigerMachenCost=`cat $stat_file_path |grep 'tiger machine spinWin pokerType'|awk -F[][] '{print $10}'|awk 'BEGIN{total=0}{totalCost+=$1}END{print totalCost}'` 
 
systemAwardTotal=`cat $stat_file_path |grep 'tiger machine spinWin pokerType' |awk -F[][] '{print $12}'|awk 'BEGIN{total=0}{totalCost+=$1}END{print totalCost}'` 
 
echo "changePokerTotalCost:$changePokerTotalCost" 
echo "spinTigerMachineCost:$spinTigerMachenCost" 
echo "systemAwardTotal:$systemAwardTotal" 
echo "kingNumber:`cat $stat_file_path |grep "pokerType[1]"|wc -l`"  
echo "baoziNumber:`cat $stat_file_path |grep "pokerType[2]"|wc -l`"  
echo "shunjinNumber:`cat $stat_file_path |grep "pokerType[3]"|wc -l`"  
echo "dilongNumber:`cat $stat_file_path |grep "pokerType[4]"|wc -l`"  
echo "jinhuaNumber:`cat $stat_file_path |grep "pokerType[5]"|wc -l`"  
echo "shunziNumber:`cat $stat_file_path |grep "pokerType[6]"|wc -l`"  
echo "duiziNumber:`cat $stat_file_path |grep "pokerType[7]"|wc -l`"  
echo "zapaiNumber:`cat $stat_file_path |grep "pokerType[8]"|wc -l`"  
echo "2huaNumber:`cat $stat_file_path |grep "pokerType[9]"|wc -l`"  
echo "1huaNumber:`cat $stat_file_path |grep "pokerType[10]"|wc -l`"  
 
exit 0 ;