If your date command don't support -d option, then use perl to calculate the passed date.
$ cat days_ago
days_ago()
{
perl -e '
# take 86400 * # of days from right now in epoch seconds
$yestertime = time - (86400 * $ARGV[0]);
$month = (localtime $yestertime)[4] + 1;
# day of the month
$day = (localtime $yestertime)[3];
# year
$year = (localtime $yestertime)[5] + 1900;
# US format mm dd yyyy
printf( "%4d%02d%02d", $year, $month, $day); ' $1
}
days_ago $1
thanks rdcwayx I was getting an error every time I tried your script.
awk: syntax error near line 1
awk: bailing out near line 1
Anyway since then I have found something else that helps. Not to sure about rules on this site but I got this from adifferent forum so thanks to p5wizard who wrote it. Hope its ok for me to detail it below.
#################################
#!/bin/ksh
# parameters
# days in year
yd=365
# days in leapyear
ld=366
# elapsed days before month x in non-leap year
mo[1]=0 mo[2]=31 mo[3]=59 mo[4]=90 mo[5]=120 mo[6]=151
mo[7]=181 mo[8]=212 mo[9]=243 mo[10]=273 mo[11]=304 mo[12]=334
# elapsed days before month x in leap year
ml[1]=0 ml[2]=31 ml[3]=60 ml[4]=91 ml[5]=121 ml[6]=152
ml[7]=182 ml[8]=213 ml[9]=244 ml[10]=274 ml[11]=305 ml[12]=335
# variables
date1=$1
date2=$2
#date1=01/01/2012
#date2=29/02/2012
leapyear()
{
# is year $1 a leapyear?
yr=$1
ly=0
if (( !(yr%4) ))
then
if (( !(yr%100) ))
then
if (( !(yr%400) ))
then
ly=1
fi
else
ly=1
fi
fi
echo ${ly}
}
julian()
{
# convert month, day to julian: day of the year
l=$1
m=$2
d=$3
if (( l==0 ))
then
((ju=mo[m]+d))
else
((ju=ml[m]+d))
fi
echo ${ju}
}
echo $date1|tr '/' ' '|read d1 m1 y1
l1=$(leapyear $y1)
j1=$(julian $l1 $m1 $d1)
echo $date2|tr '/' ' '|read d2 m2 y2
l2=$(leapyear $y2)
j2=$(julian $l2 $m2 $d2)
((diff=0))
if ((y1==y2))
then
((diff=j2-j1))
else
if ((y1<y2))
then
factor=1
else
# switch dates, negative diff
echo ${y1} ${m1} ${d1} ${l1} ${j1} ${y2} ${m2} ${d2} ${l2} ${j2}|read y2 m2 d2 l2 j2 y1 m1 d1 l1 j1
factor=-1
fi
# days until end of year of begin date
if ((l1==1))
then
((diff=ld-j1))
else
((diff=yd-j1))
fi
((y1+=1))
# keep adding yd or ld for every year or leapyear passed
while ((y1<y2))
do
l1=$(leapyear $y1)
if ((l1==1))
then
((diff+=ld))
else
((diff+=yd))
fi
((y1+=1))
done
# add julian date of end date
((diff+=j2))
# positive or negative difference?
((diff*=factor))
fi
#echo "difference between ${date1} and ${date2} is ${diff} days"
diffdays=${diff}
#################################
So what I am doing to resolve my issue is to get the dates from my file and do a loop using the above as a function and only echoing out the number of days after which doing a simple "if statement" to see if the number is greater than 30 if so ignore line if it isnt the print line to a new file.