String Length

Hi All,

One of my source file is having Date column and the format of the column is YYYY-MM-DD. As per my business logic I have to check if the date format either YYY-MM-DD or YYYY-M-DD. If any records are in this format then I have print all the records and send those invalid records through mail to user.

Ex:

CutomerId PurchasedDate Amount
 
1478210,2014-02-15,250
1345721,999-03-20,400
2678103,2010-4-10,500
3218923,2013-05-18,600

Expected Output:

CutomerId PurchasedDate Amount

1345721,999-03-20,400
2678103,2010-4-10,500

Kindly help me how can I achive above result.

$ awk -F, '/^[0-9]/ && length($2) != 10' infile
1345721,999-03-20,400
2678103,2010-4-10,500
 $ egrep ",[0-9]{3}-[0-9]{2}-[0-9]{2},|,[0-9]{4}-[0-9]-[0-9]{2}," file

1345721,999-03-20,400
2678103,2010-4-10,500

Awk

$ awk -F, 'FNR >1 && NF && $2 !~ /^((?:19|20)[0-9][0-9])-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/' <<EOF
CutomerId PurchasedDate Amount
 
1478210,2014-02-15,250
1345721,999-03-20,400
2678103,2010-4-10,500
3218923,2013-05-18,600
EOF
 
1345721,999-03-20,400
2678103,2010-4-10,500

Thanks to all. My problem resolved. Thanks to you all once again for your timely help :slight_smile: