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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
# split question pool file into separate file for each question
# Copyright (C) 2012-2015 John Nogatch <jnogatch@gmail.com>
# discard empty lines
(NF == 0){ next}
# change all tab to space
{gsub( /\t/, " ")}
# remove leading whitespace
{sub( /^[ ]*/, "")}
# remove spurious \r
{gsub( /\r/, "")}
# replace \222 apostrophes
{gsub( /\222/, "\47")}
# replace 3-char hyphen
{gsub( /\342\200\223/, "-")}
# replace \223 quotes
{gsub( /\223/, "\42")}
# replace \224 quotes
{gsub( /\224/, "\42")}
# replace \226 hyphen
{gsub( /\226/, "-")}
# replace \240 space
{gsub( /\240/, " ")}
# replace 3-char exponent leader
{gsub( / \342\200.E/, "^")}
# replace 3-char apostrophes
{gsub( /\342\200\231/, "\47")}
# replace 3-char quotes
{gsub( /\342\200\234/, "\42")}
# replace 3-char quotes
{gsub( /\342\200\235/, "\42")}
# recognize answer/distractor lines
/^[ABCD]\./{
# output answer/distractor to file
print $0 > question
next
}
# recognize answer/distractor lines (elements 7, 7R)
/^[a-f])/{
# output answer/distractor to file
print $0 > question
next
}
# anything other than answer/distractor line
{
close( question) # avoid leaving too many descriptors open
question = "" # no current output file
}
# recognize answer key lines (elements 1, 3, 8)
/^Answer Key:/{
sub( /^Answer Key:/, "")
gsub( /:/, "")
for (i = NF - 1; i > 0; i -= 2) {
print $i " " $(i+1) > "answer"
close( "answer")
system( "cat " $i " >> answer")
system( "mv answer " $i)
}
next
}
# recognize answer key lines (element 9)
/^Answers:[^-]+$/{
sub( /^Answers:/, "")
gsub( /:/, "")
for (i = NF - 1; i > 0; i -= 2) {
print "9-" $i " " $(i+1) > "answer"
close( "answer")
system( "cat 9-" $i " >> answer")
system( "mv answer 9-" $i)
}
next
}
# discard "Section-.:" line (elements 7, 7R)
/^Section-[A-Z]: /{ next}
# discard page number line (elements 7, 7R)
/^- [0-9]+ -$/{ next}
# recognize answer key lines (elements 7, 7R)
/^Answers:[ ]+[0-9]+[A-Z][0-9]+ - [A-D]/{
sub( /^Answers:/, "")
gsub( /-/, "")
for (i = NF - 1; i > 0; i -= 2) {
print element "-" $i " " $(i+1) > "answer"
close( "answer")
system( "cat " $i " >> answer")
system( "mv answer " element "-" $i)
}
next
}
# recognize new question (elements 1, 3, 8)
/^[0-9]-[0-9]*/{
question = $1 # new current output file
$1 = "" # remove # from question text
sub( /^ /, "")
# output question to file
print $0 > question
next
}
# recognize new question (elements 5 & 6)
/^[0-9][AB][0-9]*$/{
question = $1 # new current output file
if (getline != 1) print "unable to read answer line " NR # read another line & exit if error
if (NF != 1) print "invalid answer line " NR # expecting answer
print question " " $1 > question # output # & answer to file
if (getline != 1) print "unable to read question line " NR # read another line & exit if error
print $0 > question # output question text to file
next
}
# recognize new question (elements 7, 7R)
/^[0-9]+[A-Z][0-9]+ /{
question = $1 # new current output file
sub( /-/, "", question) # remove "-"
# print "e7: " $0 # display question # to help correct typos
$1 = "" # remove question # from question text
print $0 > question # output question text to question file
next
}
# recognize new question (element 7)
/^[0-9]+[A-Z][0-9]* */{
question = "7-" $1 # new current output file
sub( /^[0-9]+[A-Z][0-9]*/, "") # remove question #
print $0 > question # output complete question text to file
next
}
# recognize new question (element 9)
/^[0-9]+[A-Z][0-9]* */{
question = "9-" $1 # new current output file
sub( /^[0-9]+[A-Z][0-9]*/, "") # remove question #
print $0 > question # output complete question text to file
next
}
# display any line that did not get captured
{ print $0}
|