Cal no space delimiter

calcalendartext processing

I am an OpenBSD user and I am writing an awk script which automatically generates TeX course calendars for all courses that I teach. To obtain actual calendar out of the system I use Unix cal command. The problem is that the output of the cal command uses spaces as a delimiters which creates all sorts of problems when I apply my nawk script to it. I looked the source code for cal and seems that nothing short of hacking the source code will force cal command to use delimiter other than the space. What would be the simplest way to get calendar to look like this

        June 2012
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

Best Answer

You could use sed for that.

$ cal|sed -e '1n;s/\(..\)\(.\)/\1,\2/g'
      May 2012      
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, 31

1n prints the first line and moves to the next. The replacement then takes the chars three by three and prints the first two followed by , then the third.

Related Question