This repository has been archived by the owner on Apr 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorona
executable file
·310 lines (289 loc) · 10.2 KB
/
corona
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/sh
# Author: Andrej Smatana <[email protected]>
export POSIXLY_CORRECT=yes
export LC_NUMBERIC=en_US.UTF-8
export LC_ALL=C
print_help()
{
printf "Pouziti: corona [-h]\n"
printf " corona [FILTERS] [COMMAND] [LOG [LOG2 [...]]\n\n"
printf "COMMAND muze byt jeden z:\n"
printf " infected - spocita pocet nakazenych\n"
printf " merge - spoji nekolik souboru spolu\n"
printf " gender - vypise pocet nakazenych pro jednotliva pohlavi\n"
printf " age - vypise statistiku poctu nakazenych dle veku\n"
printf " daily - vypise statistiku osob pro jednotlive dny\n"
printf " monthly - vypise statistiku osob pro jednotlive mesice\n"
printf " yearly - vypise statistiku osob pro jednotlive roky\n"
printf " countries - vypise statistiku osob pro jednotlive zeme nakazy (krome CR)\n"
printf " districts - vypise statistiku osob pro jednotlive okresy\n"
printf " regiones - vypise statistiku osob pro jednotlive kraje\n\n"
printf "FILTER muze byt kombinace nasledujucich (kazdy maximalne jednou):\n"
printf " -a DATETIME - after : jsou uvazovany data jenom po tomto datu (vcetne tohoto data)\n"
printf " -b DATETIME - before: jsou uvazovany data jenom pred timto datem (vcetne tohoto data)\n\
DATETIME musi byt formatu YYYY-MM-DD\n"
printf " -g GENDER - jsou uvazovany pouze zaznamy osob daneho pohlavi. \n\
GENDER muze byt jenom M nebo Z\n"
printf " -s [WIDTH] - u vsech prikazu krome infected a merge vypisuje data v podobe histogramu\n\
WIDTH urcuje sirku nejdelsiho radku, tedy WIDTH musi byt cele kladne cislo!\n"
printf " -h - vypise napovedu\n"
}
awk_filter()
{
echo "id,datum,vek,pohlavi,kraj_nuts_kod,okres_lau_kod,nakaza_v_zahranici,nakaza_zeme_csu_kod,reportovano_khs"
awk -F ',' -v filter_after="$FILTER_AFTER" -v filter_before="$FILTER_BEFORE" \
-v filter_gender="$FILTER_GENDER" -v rgx_dt="${_regex_date}" -v rgx_int="${_regex_integer}" 'NR>1 {
if (!($2 ~ rgx_dt) && $2 != "datum") {print "Invalid date: " $0 | "cat 1>&2";}
else if (!($3 ~ rgx_int) && $3 != "vek") {print "Invalid age: " $0 | "cat 1>&2";}
else if ((NF <= 9 && NF > 1) && $2 >= filter_after && $2 <= filter_before && \
$4 == (filter_gender ? filter_gender : $4)) {print $0}}'
}
awk_histogram()
{
if [ "$S_FLAG" = "default" ]; then
awk -F ':' -v width="$WIDTH" '{
constant=(int($2 / width)); printf("%s:", $1);
if (constant) {printf(" ")}; while (constant) { printf("#"); constant--} printf("\n");}'
else
awk -F ':' -v s="$S_FLAG" 'BEGIN {max=0;} {
if (max<$2) {max=$2;} a[NR] = $1; value[NR] = $2;}
END { tmp=(max / s);
for (i=1; i<=NR; i++) {
constant=(int(value[i] / tmp));
printf("%s:", a[i]);
if (constant) {printf(" ");}
while (constant--) {printf("#"); constant;}
printf("\n");} }'
fi
}
awk_daily() {
awk -F ',' 'BEGIN {count=0;} NR>1 {
if (!($2 == "")) {a[$2]++;} else {count++;}}
END {for(i in a) print i ": " a[i]; if (count) {print "None: " count;}}'
}
FILTER_AFTER=""
FILTER_BEFORE="9999-12-31"
FILTER_GENDER=""
ARG_INDEX="1"
S_FLAG=""
WIDTH=""
COMMAND="off"
LOG_FILES=""
GZ_FILES=""
BZ2_FILES=""
OUTPUT=""
# https://www.regexlib.com/REDetails.aspx?regexp_id=3892 regex by Radek
_regex_date='^((([0-9]{4}-((0[13578]-|1[02]-)(0[1-9]|[12][0-9]|3[01])|(0[13456789]-|1[012]-)(0[1-9]|[12][0-9]|30)|02-(0[1-9]|1[0-9]|2[0-8])))|((([02468][048]|[13579][26])00|[0-9]{2}([13579][26]|0[48]|[2468][048])))-02-29)){0,10}$'
_regex_integer='^[0-9]*$'
while [ $# -gt 0 ]; do
key=$1
shift
case $key in
-a|--after)
if [ $# -lt 1 ]; then
>&2 echo "error: Chybajuci argument k filtru -a"
exit 1
else
if [ -z "$FILTER_AFTER" ] && [ "$ARG_INDEX" = "1" ]; then
FILTER_AFTER=$1
if ! echo "$FILTER_AFTER" | grep -qE "$_regex_date"; then
>&2 echo "error: Datum musi byt vo formate YYYY-MM-DDD"
exit 1
fi
else
>&2 echo "error: Filter -a moze byt pouzity len 1-krat a na prvej pozicii (viz. -h)."
exit 1
fi
fi
shift
;;
-b|--before)
if [ $# -lt 1 ]; then
>&2 echo "error: Chybajuci argument k filtru -b"
exit 1
else
if [ "$FILTER_BEFORE" = "9999-12-31" ] && [ "$ARG_INDEX" = "1" ]; then
FILTER_BEFORE=$1
if ! echo "$FILTER_BEFORE" | grep -Eq "$_regex_date"; then
>&2 echo "error: Datum musi byt vo formate YYYY-MM-DD"
exit 1
fi
else
>&2 echo "error: Filter -b moze byt pouzity len 1-krat a na prvej pozicii (viz. -h)."
exit 1
fi
fi
shift
;;
-g|--gender)
if [ $# -lt 1 ]; then
>&2 echo "error: Chybajuci argument k filtru -g"
exit 1
else
if [ -z "$FILTER_GENDER" ] && [ "$ARG_INDEX" = "1" ]; then
FILTER_GENDER=$1
if ! echo "$FILTER_GENDER" | grep -qE '^[M]$|^[Z]$'; then
>&2 echo "error: Parameter pohlavia moze byt len M alebo Z."
exit 1
fi
else
>&2 echo "error: Filter -g moze byt pouzity len 1-krat. a na prvej pozicii (viz. -h)."
exit 1
fi
fi
shift
;;
-s)
if [ -n "$S_FLAG" ] || ! [ "$ARG_INDEX" = "1" ]; then
>&2 echo "error: Filter -s moze byt pouzity len 1-krat a na prvej pozicii (viz. -h)."
exit 1
else
# no need to check whether the next argument exists
# check just whether there's any number and then check its validity
if ! echo "$1" | grep -qE '^[+-]?[0-9]+([.][0-9])+?$'; then
S_FLAG="default"
else
if ! echo "$1" | grep -qE '^[1-9][0-9]*$'; then
>&2 echo "error: Parameter sirky moze byt len cele kladne cislo vacsie ako 0."
exit 1
fi
S_FLAG=$1
shift
fi
fi
;;
-h|--help)
print_help
exit 1
;;
infected|merge|gender|age|daily|monthly|yearly|countries|districts|regions)
if [ "$COMMAND" = "off" ] && [ "$ARG_INDEX" = "1" ]; then
COMMAND=$key
ARG_INDEX="2"
else
>&2 echo "error: Prikaz moze byt uvedeny len raz a na druhej pozicii (viz. -h)."
exit 1
fi
;;
*.gz)
GZ_FILES="$GZ_FILES $key"
ARG_INDEX="3"
;;
*.bz2)
BZ2_FILES="$BZ2_FILES $key"
ARG_INDEX="3"
;;
*.csv)
LOG_FILES="$LOG_FILES $key"
ARG_INDEX="3"
;;
*)
>&2 printf "error: Prikaz %s je neznamy. Precitajte si manual!\n" "$key"
print_help
exit 1
;;
esac
done
if [ -n "$GZ_FILES" ] && [ -n "$BZ2_FILES" ]; then
LOG_FILES=$(gzip -d -c $GZ_FILES; bzip2 -d -c $BZ2_FILES | cat $LOG_FILES -)
elif [ -n "$GZ_FILES" ]; then
LOG_FILES=$(gzip -d -c $GZ_FILES | cat $LOG_FILES -)
elif [ -n "$BZ2_FILES" ]; then
LOG_FILES=$(bzip2 -d -c $BZ2_FILES | cat $LOG_FILES -)
elif [ -n "$LOG_FILES" ]; then
LOG_FILES=$(cat $LOG_FILES)
else
LOG_FILES=$(cat)
fi
################################ FILTERING #################################
case $COMMAND in
infected)
echo "$LOG_FILES" | tr -d '[:blank:]' | awk_filter | sed '1d' | wc -l
COMMAND="PROCESSED"
;;
merge)
echo "$LOG_FILES" | tr -d '[:blank:]' | awk_filter
COMMAND="PROCESSED"
;;
off)
echo "$LOG_FILES" |tr -d '[:blank:]' | awk_filter
COMMAND="PROCESSED"
;;
esac
if ! [ $COMMAND = "PROCESSED" ]; then
OUTPUT=$( echo "$LOG_FILES" | tr -d '[:blank:]' | awk_filter |
case $COMMAND in
age)
awk -F ',' 'BEGIN {
a[">105 "] = a["96-105"] = a["86-95 "] = a["76-85 "] = 0;
a["66-75 "] = a["56-65 "] = a["46-55 "] = a["36-45 "] = 0;
a["26-35 "] = a["16-25 "] = a["6-15 "] = a["0-5 "] = 0;}
NR>1 {
if ($3 > 105) {a[">105 "]++;}
else if ($3 > 95) {a["96-105"]++;}
else if ($3 > 85) {a["86-95 "]++;}
else if ($3 > 75) {a["76-85 "]++;}
else if ($3 > 65) {a["66-75 "]++;}
else if ($3 > 55) {a["56-65 "]++;}
else if ($3 > 45) {a["46-55 "]++;}
else if ($3 > 35) {a["36-45 "]++;}
else if ($3 > 25) {a["26-35 "]++;}
else if ($3 > 15) {a["16-25 "]++;}
else if ($3 > 5) {a["6-15 "]++;}
else if ($3 >= 0) {a["0-5 "]++;}
else {a["None "]++;}
} END{for(i in a) print i ": " a[i]}' | sort -V |
awk -F ':' '{if ($1 != "None ") {print $0;} else {a = $0;}} END {print a}'
;;
gender)
awk -F ',' 'NR>1 {if ($4 != "") {a[$4]++;} else {a["None"]++;}}
END {for(i in a) {print i ": " a[i];}}' | sort -V |
awk -F ':' '{if ($1 != "None") {print $0;} else {a = $0;}} END {print a}'
;;
daily)
awk_daily | sort
;;
monthly)
# awk_daily to print in format of "YYYY-MM-DD : NUMBER" and then process the output
awk_daily | awk -F '[- ]' '{
if (!($1 == "None:")) {a[$1 "-" $2] += $4;} else {a["None"] += $2;}}
END {for (i in a) print i ": " a[i];}' | sort
;;
yearly)
awk_daily | awk -F '[- ]' '{
if (!($1 == "None:")) {a[$1] += $4;} else {a["None"] += $2;}}
END {for (i in a) print i ": " a[i];}' | sort
;;
countries)
awk -F ',' 'NR>1 {
if (!($8 == "CZ" || $8 == "")) {a[$8]++;}}
END{for (i in a) {print i ": " a[i];}}' | sort
;;
districts)
awk -F ',' 'BEGIN {count=0;} NR>1 {
if (!($6 == "")) {a[$6]++;} else {count++;}}
END {for(i in a) print i ": " a[i]; if (count) {print "None: " count;}}' | sort
;;
regions)
awk -F ',' 'BEGIN {count=0;} NR>1 {
if (!($5 == "")) {a[$5]++;} else {count++;}}
END {for(i in a) print i ": " a[i]; if (count) {print "None: " count;}}' | sort
;;
esac
)
case $COMMAND in
age) WIDTH=10000;;
gender) WIDTH=100000;;
daily) WIDTH=500;;
monthly) WIDTH=10000;;
yearly) WIDTH=100000;;
countries) WIDTH=100;;
districts) WIDTH=1000;;
regions) WIDTH=10000;;
esac
if [ -n "$S_FLAG" ]; then
echo "$OUTPUT" | awk_histogram
else
echo "$OUTPUT"
fi
fi