linux删除指定日期前的日志文件中的记录
2012-05-31 20:18
585 查看
#!/bin/bash
#This script can only to a year of log records do operation
#时间格式必须为月+日(例如:Oct 20)
rm -r /logtime2 /logtime
touch /logtime2 /logtime /month
LogFile=/messages
month_path=/month
times=3days
############建立月份对照表##############
echo -e "1 Jan January\n2 Feb February\n3 Mar March\n4 Apr April\n5 May May\n6 June June\n7 July July\n8 Aug August\n9 Sept September\n10 Oct October\n11 Nov November\n12 Dec December">$month_path
########时间获取#################
cat $LogFile |awk '{print $1" "$2}'>logtime
Sys30daybeforeTimeMonth=$(LANG=C date -d '-$times'|awk '{print $2}') #月份
Sys30daybeforeTimeDay=$(LANG=C date -d '-$times'|awk '{print $3}') #日期
IntMonth=$(grep $Sys30daybeforeTimeMonth $month_path|awk '{print $1}')
Sys30DaysTime=$(echo "$IntMonth $Sys30daybeforeTimeDay")
#############判断##############################
NewTime=`cat /logtime |sed -e 's/ /-/g'`
for TimeDay in $NewTime;do
TimeMonth=`echo $TimeDay|awk -F'-' '{print $1}'`
NewTimeMonth=`grep $TimeMonth $month_path |awk '{print $1}'`
NewTimeDay=`echo $TimeDay|awk -F'-' '{print $2}'`
echo "$NewTimeMonth $NewTimeDay">>logtime2
done
Times=`cat /logtime2 |sed -e 's/ /-/g'`
for time1 in $Times
do
newtimemonth=$(echo $time1|awk -F'-' '{print $1}')
newtimeday=$(echo $time1|awk -F'-' '{print $2}')
newsystimemonth=$(echo $Sys30DaysTime|awk '{print $1}')
newsystimeday=$(echo $Sys30DaysTime|awk '{print $2}')
if [ $newtimemonth,$newtimeday ];then
if [ $newtimemonth -lt $newsystimemonth ];then
newtime1=`grep $newtimemonth $month_path |awk '{print $2}'`
time2=$(echo "$newtime1\ $newtimeday")
sed -in "/$time2/d" $LogFile
elif [ $newtimemonth -eq $newsystimemonth ] && [[ $newtimeday -lt $newsystimeday ]];then
newtime2=`grep $newtimemonth $month_path |awk '{print $2}'`
time3=`echo "$newtime2\ $newtimeday"`
sed -in "/$time3/d" $LogFile
fi
fi
done
本文出自 “半兽人” 博客,请务必保留此出处http://orcswang.blog.51cto.com/916297/884105
#This script can only to a year of log records do operation
#时间格式必须为月+日(例如:Oct 20)
rm -r /logtime2 /logtime
touch /logtime2 /logtime /month
LogFile=/messages
month_path=/month
times=3days
############建立月份对照表##############
echo -e "1 Jan January\n2 Feb February\n3 Mar March\n4 Apr April\n5 May May\n6 June June\n7 July July\n8 Aug August\n9 Sept September\n10 Oct October\n11 Nov November\n12 Dec December">$month_path
########时间获取#################
cat $LogFile |awk '{print $1" "$2}'>logtime
Sys30daybeforeTimeMonth=$(LANG=C date -d '-$times'|awk '{print $2}') #月份
Sys30daybeforeTimeDay=$(LANG=C date -d '-$times'|awk '{print $3}') #日期
IntMonth=$(grep $Sys30daybeforeTimeMonth $month_path|awk '{print $1}')
Sys30DaysTime=$(echo "$IntMonth $Sys30daybeforeTimeDay")
#############判断##############################
NewTime=`cat /logtime |sed -e 's/ /-/g'`
for TimeDay in $NewTime;do
TimeMonth=`echo $TimeDay|awk -F'-' '{print $1}'`
NewTimeMonth=`grep $TimeMonth $month_path |awk '{print $1}'`
NewTimeDay=`echo $TimeDay|awk -F'-' '{print $2}'`
echo "$NewTimeMonth $NewTimeDay">>logtime2
done
Times=`cat /logtime2 |sed -e 's/ /-/g'`
for time1 in $Times
do
newtimemonth=$(echo $time1|awk -F'-' '{print $1}')
newtimeday=$(echo $time1|awk -F'-' '{print $2}')
newsystimemonth=$(echo $Sys30DaysTime|awk '{print $1}')
newsystimeday=$(echo $Sys30DaysTime|awk '{print $2}')
if [ $newtimemonth,$newtimeday ];then
if [ $newtimemonth -lt $newsystimemonth ];then
newtime1=`grep $newtimemonth $month_path |awk '{print $2}'`
time2=$(echo "$newtime1\ $newtimeday")
sed -in "/$time2/d" $LogFile
elif [ $newtimemonth -eq $newsystimemonth ] && [[ $newtimeday -lt $newsystimeday ]];then
newtime2=`grep $newtimemonth $month_path |awk '{print $2}'`
time3=`echo "$newtime2\ $newtimeday"`
sed -in "/$time3/d" $LogFile
fi
fi
done
本文出自 “半兽人” 博客,请务必保留此出处http://orcswang.blog.51cto.com/916297/884105
相关文章推荐
- Linux下用cronolog切割Tomcat日志并删除指定天数前的日志记录 推荐
- Linux -- 中删除指定日期之前的文件
- Linux脚本 删除指定日期以前的文件
- CentOS Linux自动备份文件到远程FTP服务器并删除指定日期前的备份Shell脚本
- Linux下定时切割Tomcat日志并删除指定天数前的日志记录 推荐
- linux中删除指定日期之前的文件
- linux中删除指定日期之前的文件
- CentOS Linux自动备份文件到远程FTP服务器并删除指定日期前的备份Shell脚本
- linux 删除指定日期之前的文件
- linux中删除指定日期之前的文件
- linux中删除指定日期之前的文件
- Linux中删除指定日期之前的文件
- Linux自动备份文件到远程FTP服务器并删除指定日期前的备份shell
- linux 删除指定日期之前的文件
- Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录
- linux 删除指定日期之前的文件
- linux中删除指定日期之前的文件
- Linux下定时切割Mongodb数据库日志并删除指定天数前的日志记录
- Linux下定时切割Nginx访问日志并删除指定天数前的日志记录
- Linux中删除指定日期之前的文件