removing rows from text file older than certain date

Hi I need a way of removing rows from a txt file that are older than 30 days from today, going by the date in column 2, below is an example from my file. I have tried awk but don't have enough knowledge. I would really appreciate some help.

41982,15/07/2010,H833AB/0,JZ,288
41983,15/08/2010,H822AC/0,JZ,480
41984,15/08/2010,H825AC/0,JZ,480
41985,15/08/2010,H844AA/0,JZ,480
41986,15/08/2010,H887AB/0,JZ,200
41987,15/01/2012,K826AC/0,JZ,480
41988,15/01/2012,K971AF/0,JZ,100
41989,01/01/2012,Y985AE/0,JZ,100
41990,15/12/2011,Y943AB/0,JZ,80
41991,15/12/2011,Y973AB/0,JZ,100
41992,15/12/2011,Y957AB/0,JZ,150
#! /bin/bash

while IFS=',' read no dt rest
do
    row_dt=`echo $dt | awk -F'/' '{print $3$2$1}'`
    ref_dt=`date -d"-30 days" +%Y%m%d`
    [ $row_dt -ge $ref_dt ] && echo "$no,$dt,$rest"
done < inputfile
$ ./test.sh
41987,15/01/2012,K826AC/0,JZ,480
41988,15/01/2012,K971AF/0,JZ,100

Try this...

awk -F, '{split($2,a,"/");sec=mktime(a[3]" "a[2]" "a[1]" 00 00 00");diff=(systime()-sec)/(60*60*24)} diff<=30' infile

--ahamed

Hi balajesuri,

Thank you for the help, your code works on smaller files but when trying it on one of the bigger files I get the following error.

./test.sh: line 7: [: too many arguments

Any ideas?

Thanks again

---------- Post updated at 11:30 AM ---------- Previous update was at 11:28 AM ----------

Thanks for the help but I get the following errors:

awk: line 2: function systime never defined
awk: line 2: function mktime never defined

which is your os? if solaris, use nawk

--ahamed

Hi ahamed,

I am using Ubuntu server.

Check if you have gawk

--ahamed

---------- Post updated at 08:58 AM ---------- Previous update was at 08:41 AM ----------

Try this...

#!/bin/bash

ref=$( date '+%s' )
while read line
do
  val=${line#*,}; val=${val%%,*}
  year=${val:6:4}; month=${val:3:2}; day=${val:0:2}
  sec=$( date -d"$year/$month/$day" +%s )
  ((diff=(ref-sec)/(60*60*24)))
  test "$diff" -le 30 && echo $line
done < infile

--ahamed