netrom
1
Hi guys, I know it may sound silly. I have this in my script:
JD=`date +%Y +%m|cut -c "4 5 6"`
echo $JD
the JD when I run it today outputs 803. Which is 8=part of year 2008, 03 is current month.
Is there a (simple) way of having outputted: 802 (that is always the year AND PREVIOUS month??).
e.g. when comes 1.1.2009, I'd like the JD output 812.
when it's 1.4.2008, I'd like to have variable JD = 803.
Please help!
Many thanks.
This should do what you're looking for:
JD=`date "+%y %m"|awk '{$2--}$2==0{$1--;$2=12}{printf("%.2d %.2d\n", $1, $2)}'`
Regards
netrom
3
Thanks Franklin, it gets me: 08 02
Is there a way to get the output: 802 (not 08 02)?
Thanks in advance.
Change the format in the printf statement:
printf("%d%.2d\n", $1, $2)
Regards
netrom
5
Great, works, many thanks!
klohma
6
Here is another way without all the print, cut, and tr stuff
JD=$(date --date="last month" +%Y%m | cut " 4 5 6")
Works in Linux Not Solaris.
JD=`date +'%y %m'|cut -d' ' -f1,2`
try this command
Try this one
JD=`date +'%y %m'|cut -d' ' -f1,2`
Hi,
JD=`date +'%y %m'|cut -d' ' -f1,2`