$ cat tst.awk
BEGIN {
mths="JanFebMarAprMayJunJulAugSepOctNovDec"
name="Mar"; print name " -> " (match(mths,name)+2)/3
name="Sep"; print name " -> " (match(mths,name)+2)/3
nbr=3; print nbr " -> " substr(mths,(nbr*3)-2,3)
nbr=9; print nbr " -> " substr(mths,(nbr*3)-2,3)
}
$ awk -f tst.awk
Mar -> 3
Sep -> 9
3 -> Mar
9 -> Sep
这是@dmckee 的脚本修改后产生有序输出:
$ cat tst2.awk
BEGIN {
n = split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec",month)
for (i in month) {
month_nums[month[i]]=i
}
for (i=1; i<=n; i++) {
print i "\t" month[i]
}
for (i=1; i<=n; i++) {
m = month[i]
print m "\t" month_nums[m]
}
}
$ awk -f tst2.awk
1 Jan
2 Feb
3 Mar
4 Apr
5 May
6 Jun
7 Jul
8 Aug
9 Sep
10 Oct
11 Nov
12 Dec
Jan 1
Feb 2
Mar 3
Apr 4
May 5
Jun 6
Jul 7
Aug 8
Sep 9
Oct 10
Nov 11
Dec 12