Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

empty files within specific date range

Status
Not open for further replies.

visvid

Technical User
Jun 3, 2002
131
GB
Wonder if anyone can help.

I have a log which starts from Dec 4 and is constantly updated. Rather than empty the log, I want to just keep the last 7 days worth.

I have tried find . -mtime +7 , but this cmd works well when looking for files within a filesystem, I want to just edit that log file ,

I have tried some awk cmds, but unsure how to progress further .

Any ideas greatly appreciated


Regards

visvid
 
... seeing a sample log file would be a good starting point..... vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Dec 4 21:49:10 cspcws snmpd[8776]: NOTICE: SMUX relation started with (172.30.5.253+54268+985) is the start of the log

Jan 2 23:38:36 cspcws snmpd[8776]: EXCEPTIONS: simpleOpen rejected (badIdentity): enterprises.1031.1.1 (SMUX 172.30.5.253+50404+2371)

Is the end of the file

 
Visvid:

The shell is poor when it comes to date arithmetic; It needs help. It's easier to do date arithmentic if you can track an integer number of days, minutes, or seconds from a give point in time such as the unix Epcoh - Jan 1, 1970, midnight UTC. Instead of the Epoch I can easily implement the Julian date algorithm which counts the days from
1 Jan 4713. See URL:

Basically, this is what the script does:
1) determine the JD of the current system date,
2) determine the JD of the date to save - (system date JD - $1)
3) Read each line of the log file
4) determine the JD of the log date (1st two fields)
5) if the JD is within range save, it to the newlog.file

This ksh script contains no error checking, but should be documented
well enough to give you the idea. (I use ksh because it's easier
to do arithmetic than with sh).

Regards,

Ed

#!/bin/ksh
# jd.ss:
# $1 is the number of days to save - default to 7 days. NO ERROR CHECKING

# The Julian date (JD) is a continuous count of days from 1 January
# 4713 BC. The following algorithm is good from years 1801 to 2099
# See URL: for
# more information
# This function takes day, month, and year as arguments and returns
# the Julian date from 1 Jan 4713 BC. NO ERROR CHECKING
# $1 - day
# $2 - month
# $3 - year
get_JD ()
{
JDD=$(($1-32075+1461*($3+4800+($2-14)/12)/4+367*($2-2-($2-14)/12*12)/12-3*(($3+4900+($2-14)/12)/100)/4))
echo $JDD
}

return_month_no ()
{
if [[ $1 = &quot;Jan&quot; ]]
then
echo 1
elif [[ $1 = &quot;Feb&quot; ]]
then
echo 2
elif [[ $1 = &quot;Mar&quot; ]]
then
echo 3
elif [[ $1 = &quot;Apr&quot; ]]
then
echo 4
elif [[ $1 = &quot;May&quot; ]]
then
echo 5
elif [[ $1 = &quot;Jun&quot; ]]
then
echo 6
elif [[ $1 = &quot;Jul&quot; ]]
then
echo 7
elif [[ $1 = &quot;Aug&quot; ]]
then
echo 8
elif [[ $1 = &quot;Sep&quot; ]]
then
echo 9
elif [[ $1 = &quot;Oct&quot; ]]
then
echo 10
elif [[ $1 = &quot;Nov&quot; ]]
then
echo 11
elif [[ $1 = &quot;Dec&quot; ]]
then
echo 12
else
echo 0 # error
fi

}

# get the current year, month, day, and determine the Julian date
TY=&quot;20&quot;$(date '+%y') # Year
TM=$(date '+%m') # Month
TD=$(date '+%y') # Day
JD=$(get_JD TD TM TY) # Julian Date

if [[ $# -eq 0 ]]
then # default to 7 days
daydiff=7
else
daydiff=$1
fi

# go back the number of days to save
backJD=$((JD-daydiff))

while read line
do
# strip off the first 2 fields of the data file
fields=$(echo $line|awk ' { printf(&quot;%s %s\n&quot;, $1, $2) } ')
set - $(echo $fields) # parse the month and day
mnum=$(return_month_no $1) # change month string to number
# since data file doesn't have a year, make the gross assumption
# if the data file month is greater than current month, the year
# is from last year.
if [[ $mnum -gt $TM ]]
then
yr=$((TY-1))
else
yr=$TY
fi
logJD=$(get_JD $2 mnum yr) # Julian Date for each record
# if log date JD >= backJd, save the record to the new log file
if [[ logJD -ge backJD ]]
then
echo $line >> newlog.file
fi
done < log.file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top