#!/bin/sh

# Takes in a series of monthly values as CSV "YYYY-MM-DD,value",
# converts to month offsets from the start of the first value's year,
# discards second and further values in any one month,
# and fills in missing month rows with blanks.

##########
# May be used / adapted / etc without any promise of fitness for purpose
# under the terms of the Apache License Version 2.0, January 2004
#     http://www.apache.org/licenses/LICENSE-2.0
##########

# TODO: could have this drop multiple values for the same month.

# Input is of the form:
#2008-08-30,19
#2008-11-19,14
#2008-12-03,10

# Output is of the form:
# 
#
#
#
#
#
#
#19
#
#
#14
#10

awk -F, '{
    YYYY=substr($1,1,4)+0;
    MM=substr($1,6,2)+0;
    MONTHS=12*YYYY+MM;
    if(!(FIRSTM>0)) { FIRSTM = 12*YYYY; }
    M = MONTHS - FIRSTM;
    if(LASTM == M) { next; } # Discard all beyond 1st value for a month.
    #print M;

    # Fill in any missing months with just the month, no value.
    while(LASTM < M - 1) { print ""; ++LASTM; }

    print $2

    LASTM = M;
    }'
