-
Notifications
You must be signed in to change notification settings - Fork 0
/
wardscrape
executable file
·92 lines (71 loc) · 2.52 KB
/
wardscrape
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
#!/bin/sh
# usage wardscrape file
# where file is the html body of an election report
cat $1 | tr -d '\011\015,' | sed -e 's/<[^>]*>/ /g' -e 's/ */ /g' -e 's/^ //' -e '/^ *$/d' | awk '
# Precinct+Ward summary in first 12 lines
NR == 4 { Ward = $6; Precinct = $8}
NR == 7 {RegisteredVoters = $1}
NR == 9 {BallotsCast = $1}
NR == 11 {VoterTurnout = $1}
NR == 13 {print "Ward," Ward ",Precinct," Precinct ",Registered Voters," RegisteredVoters ",Ballots Cast," BallotsCast ",Voter Turnout," VoterTurnout}
NR < 13 {next}
# new race starts with "Vote for "
/Vote For / { # first, clean up from prior race
if (ThisRace) { # ... unless this is the first race, i.e.,ThisRace has never been set
RaceTally[ThisRace]++ # how many times we have seen ThisRace reported. it can be more than 1 #alas
if ((Candidates - WriteIns) <= 1) # sometimes there are no candidates, sometimes only write-ins
Unopposed[ThisRace]++ # keep track of how many times ThisRace appears to be unopposed
}
}
# Race we care about
/Vote For 1 / {
# new race that we care about
skipping = 0
Candidates = 0
WriteIns = 0
# combine primaries, whose first field is "D" or "R" or "DEM" or "REP", into a single race
firstfield = 1
if ($1 == "DEM" || $1 == "REP" || $1 == "D" || $1 == "R") # skip first field if it is a party designator
firstfield = 2
ThisRace = $firstfield
for (i = firstfield + 1; i <= NF-5; i++) # last 5 fields are data, the rest are the name of the race.
ThisRace = ThisRace " " $i
next
}
# Race we do not care about
/Vote For / {
skipping = 1
}
# Skip until we start to care again
skipping {next}
# look at the vote counts
#is it a partisan race? look for a DEM or a REP
/(DEM)/ {IsPartisan[ThisRace] = 1}
/(REP)/ {IsPartisan[ThisRace] = 1}
/[Ww]rite-[Ii]n/ {WriteIns++}
# sum up the votes and candidates
{
field = NF - 1 # this is the number of votes
results[ThisRace] += $field
Candidates++
next
}
END { # clean up the final race (just like above, q.v. for comments)
if (ThisRace) {
RaceTally[ThisRace]++
if ((Candidates - WriteIns) <= 1)
Unopposed[ThisRace]++
}
# output time!
for (i in results) {
race = i
if (IsPartisan[i] == 1)
race = race "@" # kludge to let the downstream script know this race is partisan
if (Unopposed[i] == RaceTally[i]) { # race is unopposed if every time we saw it, it was unopposed
if (verbose == 0)
continue # not really interested in unopposed races
race = race "+" # kludge to let the downstream know this race is unopposed
}
print race "," results[i]
}
}'