hi all
searched google and here, cant find and am begining to suspect there is no options for this.
shell = born
with either the date or cal command I need to display the number of days in current month. can anyone point me in the right direction?
cal | nawk 'FNR>2{d+=NF}END{print d}'
rontopia:
thanks..
I get nawk not found
so use 'awk' instead.....
sorry I got it now.. thanks a bunch for the help
How about:
days=$(($(cal | wc -w) - 9))
Takes away 9 because of 7xDoW_heading + 1xmonth + 1xyear
or pull last word on the last line with sed:
days=$(cal | sed -n '/[23]/h;${x;s/.* //p;}')
ctsgnb
March 15, 2011, 7:34pm
7
# cal | sed 'N;N;N;N;N;N;N;s/\n//g;s/.*\(..\)$/\1/'
31
# cal | awk 'NF{x=$NF}END{print x}'
31
---------- Post updated at 12:34 AM ---------- Previous update was at 12:33 AM ----------
# cal | xargs -n1 | tail -1
31
@Schubler : in some case, yours sed solution doesn't work as expected :
# cal 7 2011 | sed 'N;N;N;N;N;N;N;s/\n//g;s/.*\(..\)$/\1/'
31
# cal 7 2011 | sed -n '/[23]/h;${x;s/.* //p;}'
#
i got the same behaviour in linux & solaris
Some others:
# v=$(cal)
# echo ${v##* }
31
# echo $(cal) | tail -c 3
31
1 Like
hehehe, so many ways to skin a cat.
@ctsgnb , thanks was my sed solution - fix was pretty simple:
# cal 7 2011 | sed -n '/[23]/h;${x;s/.* //;p}'
31
ctsgnb
March 15, 2011, 8:09pm
10
Hehe yes,
@Schubler ,
Your fix works on linux... but it requires an additional little semicolon fix to work on the solaris 10 i have tryied (i think FreeBSD has the same kind of need of ending semicolon before "}" ... so in fact better to keep both of the semicolon before and after the p
$ uname -a
SunOS xxxxxx 5.10 Generic_141414-01 sun4u sparc SUNW,Sun-Fire-V490
$ cal 7 2011 | sed -n '/[23]/h;${x;s/.* //;p}'
sed: command garbled: /[23]/h;${x;s/.* //;p}
$ cal 7 2011 | sed -n '/[23]/h;${x;s/.* //;p;}'
31
$
another one for the fun ...
$ echo $(cal) | tail -c 3
31
or if running solaris
$ echo $(cal) | /usr/xpg4/bin/tail -c 3
31
Using FreeBSD Man Page for date (FreeBSD Section 0) - The UNIX and Linux Forums
# uname
FreeBSD
# date
Tue Mar 15 21:05:40 EDT 2011
# date -v 1d -v +1m -v -1d "+%A %B %d %Y" # First day of (current month+1) minus 1 day.
Thursday March 31 2011
# date -v 1d -v +1m -v -1d "+%d"
31