I'm trying to calculate the previous and next months (and year but that was trivial) for a given date. I'm using this for the next month:
$ awk 'BEGIN{m=12; print m%12+1}'
1
which is pretty tight IMHO and for the previous:
$ awk 'BEGIN{m=1; print (m+10)%12+1}'
12
̲w̲h̲i̲c̲h̲ ̲d̲o̲e̲s̲ ̲t̲h̲e̲ ̲j̲o̲b̲ ̲b̲u̲t̲ ̲w̲h̲i̲c̲h̲ ̲I̲'̲m̲ ̲n̲o̲t̲ ̲h̲a̲p̲p̲y̲ ̲w̲i̲t̲h̲, I feel it could be in tighter form but just can't figure it out ATM.
So, in which ways would you calculate the previous and next months (and years)? Here's a test template:
$ awk '{
y=substr($1,1,4) # extract yyyy from yyyymmdd
m=substr($1,5,2)+0 # extract mm from yyyymmdd
print "this:", m, y,
"next:", m%12+1,
y+(m==12),
"prev;", (m+10)%12+1, # optimize me
y-(m==1)
}' <(echo -e 20190116 \\n 20181231)
this: 1 2019 next: 2 2019 prev; 12 2018
this: 12 2018 next: 1 2019 prev; 11 2018