RosettaCodeData/Task/Five-weekends/AWK/five-weekends.awk

57 lines
1.6 KiB
Awk

# usage: awk -f 5weekends.awk cal.txt
# Filter a file of month-calendars, such as
# ...
## January 1901
## Mo Tu We Th Fr Sa Su
## 1 2 3 4 5 6
## 7 8 9 10 11 12 13
## 14 15 16 17 18 19 20
## 21 22 23 24 25 26 27
## 28 29 30 31
# ...
## March 1901
## Mo Tu We Th Fr Sa Su
## 1 2 3
## 4 5 6 7 8 9 10
## 11 12 13 14 15 16 17
## 18 19 20 21 22 23 24
## 25 26 27 28 29 30 31
# ...
# This file is generated by a script for the unix-shell,
# see http://rosettacode.org/wiki/Five_weekends#UNIX_Shell
BEGIN { print("# Month with 5 weekends:")
badYears = numW5 = 0;
lastW5 = -1
}
0+$2>33 { if( $2>currYear ) { # calendar-header: month, year
if( lastW5==numW5 ) {
badYears++; sep="\n"
if( badYears % 10 ) { sep=" " }
bY=bY currYear sep; # collect years in string
##print badYears,":", currYear
}
lastW5=numW5
}
WE=0; currYear=$2; currMY = $1 " " $2;
##print currMY;
next
}
/^Mo/ { next } # skip lines with weekday-names
{ $0 = substr($0,13) } # cut inputline, leave Fr,Sa,Su only
NF>2 { WE++; # 3 fields left => complete weekend found
if( WE>4 ) {
numW5++; printf("%4d : %s\n", numW5, currMY)
}
}
END { print("# Found", numW5, "month with 5 weekends.")
print("# Found", badYears, "years with no month having 5 weekends:")
print(bY)
}