Delete lines in a file by date on the line

Hi

I have a file with lines ending with a date in format dd/mm/yyyy see example below:

a|b|c|08/01/2011
d|a|e|31/11/2010
e|d|f|20/11/2010
f|s|r|18/01/2011

What I would like to do is delete all lines with a date older than 30 days.

With above example I should be left with a file like below

a|b|c|08/01/2011
f|s|r|18/01/2011

:wall:

Your help would be appreciated.

Thanks in advance.

awk -F"|" -vD=$(date -d '-30 days' '+%d/%m/%Y') '{if($NF<D) print $0}'  infile

1 Like

Hi Posner thanks for trying but I get the following errors

date: illegal option -- d
date: illegal option -- 3
date: illegal option -- 0
date: illegal option -- 
date: illegal option -- d
date: invalid arguement -- ys
usage: date [-u] mmddHHMM[ [cc]yy] [.SS]
          date [-u] [+format]
          date -a [-]sss[.fff]

I dont know if this help but I am on an aix box using korn shell.

Thanks.

---------- Post updated at 08:14 PM ---------- Previous update was at 07:58 PM ----------

Anyone out there please help.

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
awk -F "[|\/]" -vD=$(./days_ago 30) '$NF$(NF-1)$(NF-2)>D'  infile
1 Like

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.

For Solaris, replace awk by /usr/bin/nawk or /usr/xpg4/bin/awk