Linux删除文件有日志记录吗,linux删除指定日期前的日志文件中的记录

#!/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值