您的位置:首页 > 运维架构 > Linux

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: