A somewhat crude example:
Here you set the variable m
to the dates you want to include and their order; as a comma separated string. As per example below that would be:
m=2016-10,2016-09
Which in turn gives:
Name 2016-10 2016-09
This require names to be unique and without spaces …
awk -v m=2016-10,2016-09 '
NR==1{next}
{
# Set array x[name][month]=marks
x[$2][$1]=$3
}
END {
split(m, k, ",")
printf "Name"
for (v in k)
printf "\t%s", k[v]
for (e in x) {
printf "\n%s", e
for (v in k)
printf "\t%s", x[e][k[v]]
}
print ""
}
' data
Sample output:
Name 2016-10 2016-09
Steve 83 39
Mark 71 38
John 64 47
Sam 58 77
By column -t
:
Name 2016-10 2016-09
Steve 83 39
Mark 71 38
John 64 47
Sam 58 77
If this is a one time thing, and your data is as per example (ordered, only two months etc.) then this could also do:
awk 'NR==1{next}NR%2{print $3;next}{printf "%s\t%s\t",$2,$3}' data
datamash -WH groupby "Name" collapse "Marks" < data
OTOH if the input is coming from a database, it's hard to imagine that it couldn't be better done with a nativeselect
...group by
query.