Valid and invalid date in the file

Hi All,

How to validate the 4th column,it is date column in the file, if it valid move to valid file else moved invalid file.

9f680174-cb87|20077337254|0|20120511|N 
9f680174-cb88|20077337254|0|20120534|N

i want two file valid.txt and invalid.txt

Thanks,

#!/usr/bin/perl

use Time::Local;
use strict;

open (VF,">valid-file")  || die "$! \n";
open (IF,">Invalid-file") || die "$! \n";

while (<DATA>) {
chomp;
my @flds=split(/\|/);
eval {
        timelocal(0,0,0,substr($flds[3],6,2),substr($flds[3],4,2),substr($flds[3],0,4));
     };
print IF "$_\n" if ($@);
print VF "$_\n" if !($@);
}

close(VF);
close(IF);

__DATA__
9f680174-cb87|20077337254|0|20120511|N
9f680174-cb88|20077337254|0|20120534|N

Thanks... Do you any idea with AWK command...to validate the date column..

most versions of awk don't have very good date-handling functions, so it'd mean rolling your own and worrying about leap years and other such mess. perl is probably the best way.

Try...

awk -F '|' '{if(system("touch -t " $4 "0000 tempfile 2>/dev/null"))print > "invalid.txt"; else print > "valid.txt"}' file

how about this ?

gawk  -F"|" '{
   if (match($4,/^((19|20)[0-9][0-9])(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[0-1])$/,a)) {
      year=a[1]+0
      mon=a[3]+0
      day=a[4]+0
        if (day ==  31 && (mon == 4 ||  mon == 6 || mon == 9 || mon == 11))
        print $0 >  "valid.txt"
        else if (day >=  30 && mon == 2)
        print $0 >  "valid.txt"
        else if (mon == 2 && day ==  29 && ! (  year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
        print $0 > "valid.txt"
        else
        print $0 > "valid.txt"
   }  else { print $0 > "invalid.txt" }
   }' input_file

With GNU date:

#!/bin/bash

while IFS='|' read a b c d e
do
    date -d "$d" > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
        echo "$a|$b|$c|$d|$e" >> valid.txt
    else
        echo "$a|$b|$c|$d|$e" >> invalid.txt
    fi
done < inputfile

Using GNU awk date functions:

gawk -F\| '{t=mktime(substr($4,1,4)" "substr($4,5,2)" "substr($4,7,2)" 00 00 00")
if(t>0 && strftime("%Y%m%d",t)==$4)
    print > "valid.txt"
else
    print > "invalid.txt"}' infile