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 = "Jan" ]]
then
echo 1
elif [[ $1 = "Feb" ]]
then
echo 2
elif [[ $1 = "Mar" ]]
then
echo 3
elif [[ $1 = "Apr" ]]
then
echo 4
elif [[ $1 = "May" ]]
then
echo 5
elif [[ $1 = "Jun" ]]
then
echo 6
elif [[ $1 = "Jul" ]]
then
echo 7
elif [[ $1 = "Aug" ]]
then
echo 8
elif [[ $1 = "Sep" ]]
then
echo 9
elif [[ $1 = "Oct" ]]
then
echo 10
elif [[ $1 = "Nov" ]]
then
echo 11
elif [[ $1 = "Dec" ]]
then
echo 12
else
echo 0 # error
fi
}
# get the current year, month, day, and determine the Julian date
TY="20"$(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("%s %s\n", $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