1752509 Members
4919 Online
108788 Solutions
New Discussion

Re: help- awk

 
SOLVED
Go to solution
viseshu
Frequent Advisor

help- awk

Hi,
I want to check a date format using awk. The input will be in the format 20090430.How do i check if this date given is valid?Im left with no clues on the checks using awk.
1 REPLY 1
Dennis Handly
Acclaimed Contributor
Solution

Re: help- awk

Starting with YYYYMMDD, you can split into 3 fields:
echo 20090430 | awk '
{
year = substr($0, 1, 4)
month = substr($0, 5, 2)
day = substr($0, 7, 2)
print "year=" year, "month=" month, "day=" day
shell_command = "echo $(cal " month " " year ")"
shell_command | getline
close(shell_command)
last_day = $NF
print "The last day in", year, month, "is", last_day
}
'

Once you have the fields, you can do the range checking and let cal(1) do the the days in each month and leap year checking.