把tomcat里面的项目生成的日志进行分析切割和分类

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
clear

logs_path="/var/log/tomcat"
pro_logs_path="/path/tomcatlog/processed"
ratedate=$(date -d "Yesterday" +"%Y-%m-%d")
app_grp=(s1 s2 s3 s4 s5)
function log_cut()
{
	for i in ${app_grp[@]}
	do
		for j in $(cd $logs_path ; echo $i.log.*)
		do
			[ $j = $i.log.\* ] && continue
			ratemonth=`echo ${j##*.}|awk -F '-' '{print $1"-"$2}'`
			[ ! -f $pro_logs_path/$i\_$ratemonth.txt ] && touch $pro_logs_path/$i\_$ratemonth.txt
			if [ "s1" = $i -o "s2" = $i ] ; then
				[ $ratedate != ${j##*.} ] && grep -v 'INFO' $logs_path/$j >> $pro_logs_path/$i\_$ratemonth\.txt || grep -v 'INFO' $logs_path/$j >> $pro_logs_path/$i\_$ratedate\.txt
			elif [ "s3" = $i ] ; then
				[ $ratedate != ${j##*.} ] && egrep -v 'INFO|TransServlet.java:36' $logs_path/$j >> $pro_logs_path/$i\_$ratemonth\.txt || egrep -v 'INFO|TransServlet.java:36' $logs_path/$j >> $pro_logs_path/$i\_$ratedate\.txt
			elif [ "s4" = $i ] ; then
				[ $ratedate != ${j##*.} ] && egrep -v 'INFO|InceptData=true' $logs_path/$j >> $pro_logs_path/$i\_$ratemonth\.txt || egrep -v 'INFO|InceptData=true' $logs_path/$j >> $pro_logs_path/$i\_$ratedate\.txt
			elif [ "s5" = $i ] ; then
				[ $ratedate != ${j##*.} ] && egrep 'threw|at ' $logs_path/$j >> $pro_logs_path/$i\_$ratemonth\.txt || egrep 'threw|at ' $logs_path/$j >> $pro_logs_path/$i\_$ratedate\.txt
			fi
			rm $logs_path/$j
		done
	done
}

function hmd_cut()
{
	for k in $(cd $logs_path ; echo *0000.txt)
	do
		mddate=${k:2:6}
		if [ $mddate != $(date -d "last month" +"%Y%m") -a $mddate != $(date +"%Y%m") ] ; then
			ls -lh $logs_path/$k|awk '{print $NF,$5}'|sed 's/\/var\/log\/tomcat\///g' >> $pro_logs_path/hmd.txt
			rm $logs_path/$k
		fi
	done
}

function txt_clear_up()
{
for w in ${app_grp[@]}
do
	for tt in $(cd $pro_logs_path ; echo $w.*)
	do
		ratemonth=`awk -F '-|\_' '{print $2"-"$3}' $tt`
		[ $tt != $w\_$ratemonth\.txt -a $tt != $w\_$ratedate\.txt ] && cat $pro_logs_path/$tt >> $pro_logs_path/$w\_$ratemonth\.txt
		rm $pro_logs_path/$tt
	done
done
}

log_cut
hmd_cut
txt_clear_up
#ls -lh $pro_logs_path

编程技巧