Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Validation

Status
Not open for further replies.

knarum

Programmer
Feb 2, 2001
1
US
How can I validate dates in varying format and replace invalid dates with default values in a file. (ie. knowing that the format is YYYYMMDD and the 3rd column is supposed to be a date, output the date only if it is valid otherwise output a default date)
 
knarum-

This may do what is required.

I used a slash and dash which are the normal
separators for dates and a colon for a field
separator.

You will have to substitute whatever delimiters
and such that you have in your data for what I
have used.

Yes, the if statement is real ugly, but it works!


#! /bin/sh

awk 'BEGIN{FS = OFS = ":"}

{
split ($3,slash,"/")
split ($3,dash,"-")

if (((slash[3] > 0)&amp;&amp;(slash[3] < 32))||((dash[3] > 0)&amp;&amp;(dash[3] < 32))) print
else { $3 = &quot;2000/01/01&quot; ; print }

}' dates > out

more out

# end of shell file



file &quot;dates&quot; shown below:

1:2:99/06/01:4:5
2:2:98-03-19:4:5
3:2:3-19-98:4:5
4:2:95/06/03:4:5
5:2:95/6/3:4:5
6:2:3/6/95:4:5

file &quot;out&quot; shown below:

1:2:99/06/01:4:5
2:2:98-03-19:4:5
3:2:2000/01/01:4:5
4:2:95/06/03:4:5
5:2:95/6/3:4:5
6:2:2000/01/01:4:5


Hope this helps.


flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top