Code:
$ cat unknown.awk
BEGIN { FS="\""
OFS="," }
# Don't repeat the same tail if we don't recognize something.
# Substitute "2010-12-21 14:00" into "2010-12-21,14:00"
{ TAIL=""; sub(/ /, ",", $1) }
# Whenever we see a type we recognize, assemble a 'tail' to append to the line
$3 == "Subline" {
VAL=substr($5, 9, 20);
sub(/ *$/, "", VAL); # Strip off extra spaces
TAIL=" " OFS " " OFS VAL OFS " " OFS " \"";
}
$3 == "Nameline" {
FNAME=substr($5, 12, 8);
LNAME=substr($5, 32, 9);
sub(/ *$/, "", FNAME);
sub(/ *$/, "", LNAME);
TAIL=LNAME OFS FNAME OFS " "OFS" "OFS" \""
}
$3 == "Markline" {
TAIL=" , , ," substr($5, 11, 2) "," substr($5, 26, 2) "\""
}
# Print the line with fields in the new order and tail appended.
{
print $1 OFS $3 OFS $4 OFS TAIL
}
$ awk -f unknown.awk data
2010-12-21,14:00,Nameline,Midterm,Doe,Jane, , , "
2010-12-21,14:00,Subline,Midterm, , ,Mathematics, , "
2010-12-21,14:00,Markline,Midterm, , , ,48,92"
2010-12-21,14:00,Subline,Midterm, , ,Physics, , "
2010-12-21,14:00,Markline,Midterm, , , ,30,86"
2010-12-21,14:00,Subline,Midterm, , ,Chemistry, , "
2010-12-21,14:00,Markline,Midterm, , , ,45,98"
2011-04-22,09:00,Nameline,Final,Smith,John, , , "
2010-04-22,09:00,Subline,Final, , ,Mathematics, , "
2010-04-22,09:00,Markline,Final, , , ,30,76"
2010-04-22,09:00,Subline,Final, , ,Physics, , "
2010-04-22,09:00,Markline,Final, , , ,32,70"
2010-04-22,09:00,Subline,Final, , ,Chemistry, , "
2010-04-22,09:00,Markline,Final, , , ,41,88"
$