Removing rows from a file based on date comparison

I have a '|' delimited file and want to remove all the records from the file if the date is greater than a year from sysdate. The layout of the file is as below -

xxxxxxxxxxxxxx|yyyyyy|zzzzzz|2009-12-27-00:00| 000000000|N
xxxxxxxxxxxxxx|yyyyyy|zzzzzz|2010-01-03-00:00| 000000000|N
xxxxxxxxxxxxxx|yyyyyy|zzzzzz|2010-11-10-00:00| 000000000|N
xxxxxxxxxxxxxx|yyyyyy|zzzzzz|2010-12-17-00:00| 000000000|N

As you can see above, we need to pick the year, month and day from the timestamp field from the file and do the comparison if if it greater than a year from the current date, then delete that record.

From the data given above, the third and fourth records will need to be deleted and the first and the second ones are to be retained.

Any pointers/directions is greatly appreciated..

Thanks,
Max

SECS_IN_YEAR=-31536000
while read $line
do
  DIFF=`echo $line | awk -F"|" '{ split($4,a,":");print systime() - mktime(a[1]" "a[2]" "a[3]" 00 00 00") }'`
  if [ $DIFF -gt $SECS_IN_YEAR ]
  then
     echo $line >> outputfile
  fi
done < inputfile
#!/bin/sh
ONEYEARFROMNOW=$(date  -d "now + 1 year" +'%Y%m%d%H%M')
while read line; do
  recorddate=$(echo $line|cut -f4 -d'|'|tr -d ':-' )
  if [ $recorddate -lt $ONEYEARFROMNOW ]; then
    echo "$line"
  fi
done < infile

This is using GNU date

How about:

awk -F "|" -v d=$(date "+%Y%m%d") '{split($4,a,"-");s=a[1]a[2]a[3]}s-d<10000' file

---------- Post updated at 19:44 ---------- Previous update was at 18:26 ----------

This seems to be more logical, the year is decreased with one:

awk -F "|" -v d=$(date "+%Y%m%d") '{split($4,a,"-");s=(a[1]-1)a[2]a[3]}s<d' file

Franklin, this works perfect, thanks !!

Thanks All for your quick response.

  • Max...