unix -- get number of business days
calendar, date, unix
Solution
This can make it:
cal -h | cut -c 4-17 | tail -n +3 | wc -w
Explanation
`cal` shows line on columns of 4 characters. `-h` turns off highlighting of today.
April 2014
Su Mo Tu We Th Fr Sa
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
Since Sunday is first column and Saturday the last one, it is a matter of extracting the numbers in between character 4 and 17:
$ cal -h | cut -c 4-17
April 2014
Mo Tu We Th Fr
1 2 3 4
7 8 9 10 11
14 15 16 17 18
21 22 23 24 25
28 29 30
And then remove the two first lines:
$ cal -h | cut -c 4-17 | tail -n +3
1 2 3 4
7 8 9 10 11
14 15 16 17 18
21 22 23 24 25
28 29 30
And then count the number of words:
$ cal -h | cut -c 4-17 | tail -n +3 | wc -w
22
Problem
I'm using AIX with the ksh. I'm trying to calculate the number of business days in a month. I was trying to use AWK but I'm hung up now. Here's what I've got so far: ``` cal | awk '{print $2, $3, $4, $5, $6} | awk 'NR > 2' ``` Here's the normal output (for current date/time) for cal: ``` April 2014 Sun Mon Tue Wed Thu Fri Sat 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 ``` Here's what I'm getting with that script: ``` 2 3 4 5 7 8 9 10 11 14 15 16 17 18 21 22 23 24 25 28 29 30 ``` My plan is to, in the end, read these lines into a c program and output the total weekdays (right now we're not worried about holidays on normal business days). As you can see here, since the 3rd line of output is only 5 columns itself, it's dropping the 1st business day of that week. Maybe I'm going about this whole thing wrong -- if there's another way to do this on the Unix operating system or through C: I have no sentimental attachment to "cal" and "awk" (not yet anyway).