Code:
$ cat serial.sh
awk '
BEGIN{
OFS="\t"
printf RS OFS "Enter Year till which you want to generate serial number : "
getline stop < "-"
printf OFS "Enter Output Filname : "
getline out < "-"
nonleap = "31,28,31,30,31,30,31,31,30,31,30,31"
leap = "31,29,31,30,31,30,31,31,30,31,30,31"
split(nonleap,NLP,",")
split(leap,LP,",")
if(!stop){printf RS "\t" "No User Input...Exiting" RS RS;exit }
}
function serial(start,m,d,y){
for(s=start;s<=100;s++)
if(out){
print y sprintf("%.2d",m) sprintf("%.2d",d) sprintf("%.3d",s) >out
}
else
{
print y sprintf("%.2d",m) sprintf("%.2d",d) sprintf("%.3d",s)
}
}
{
ser=substr($1,8,10)
day=substr($1,7,2)
mon=substr($1,5,2)
year=substr($1,0,4)
if(stop~/[A-Za-z]/){print "Year should be Integer...Exiting" RS;exit}
if(stop%1!=0){print "Year cannot be float...Exiting" RS;exit}
if(stop<year){print "Year should be Equal or Greater than Existing Serial Number...Exiting" RS;exit}
f=1
for(y=year;y<=stop;y++){
mon=(f==1)?mon:1
f=0
g=1
for(m=mon;m<=12;m++)
{
dend=((y%4==0) || (y%100==0) || (y%400==0))?LP[m]:NLP[m]
day=(g==1)?day:1
ser=(g==1)?ser:0
for(day=day;day<=dend;day++){serial(ser,m,day,y)}
g=0
}
}
}' file