-
Notifications
You must be signed in to change notification settings - Fork 0
/
corona
436 lines (406 loc) · 15.8 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env bash
###############################################
# #
# Name: Corona - positive people analyzer #
# Author: Adam Ližičiar, xlizic00 #
# #
###############################################
ages=( 0 0 0 0 0 0 0 0 0 0 0 0 0 ) # store ages of people, last one is none
genders=( 0 0 0 ) # man, woman, none
countries=( "AF" "AX" "AL" "DZ" "AS" "VI" "AD" "AO" "AI" "AQ" "AG" "AR" "AM" "AW" "AU" "AZ" "BS" "BH" "BD" "BB" "BE" "BZ" "BY" "BJ" "BM" "BT" "BO" "BQ" "BA" "BW" "BV" "BR" "IO" "VG" "BN" "BG" "BF" "BI" "CK" "CW" "TD" "ME" "CZ" "CN" "DK" "DM" "DO" "DJ" "EG" "EC" "ER" "EE" "ET" "FO" "FK" "FJ" "PH" "FI" "FR" "GF" "TF" "PF" "GA" "GM" "GH" "GI" "GD" "GL" "GE" "GP" "GU" "GT" "GG" "GN" "GW" "GY" "HT" "HM" "HN" "HK" "CL" "HR" "IN" "ID" "IQ" "IR" "IE" "IS" "IT" "IL" "JM" "JP" "YE" "JE" "ZA" "GS" "SS" "JO" "KY" "KH" "CM" "CA" "CV" "QA" "KZ" "KE" "KI" "CC" "CO" "KM" "CD" "CG" "KP" "KR" "XK" "CR" "CU" "KW" "CY" "KG" "LA" "LS" "LB" "LR" "LY" "LI" "LT" "LV" "LU" "MO" "MG" "HU" "MY" "MW" "MV" "ML" "MT" "IM" "MA" "MH" "MQ" "MU" "MR" "YT" "UM" "MX" "FM" "MD" "MC" "MN" "MS" "MZ" "MM" "NA" "NR" "DE" "NP" "NE" "NG" "NI" "NU" "NL" "NF" "NO" "NC" "NZ" "OM" "PK" "PW" "PS" "PA" "PG" "PY" "PE" "PN" "CI" "PL" "PR" "PT" "AT" "RE" "GQ" "RO" "RU" "RW" "GR" "PM" "SV" "WS" "SM" "SA" "SN" "MK" "MP" "SC" "SL" "SG" "SK" "SI" "SO" "AE" "US" "RS" "CF" "SD" "SR" "SH" "LC" "BL" "KN" "MF" "SX" "ST" "VC" "SZ" "SY" "SB" "ES" "SJ" "LK" "SE" "CH" "TJ" "TZ" "TH" "TW" "TG" "TK" "TO" "TT" "TN" "TR" "TM" "TC" "TV" "UG" "UA" "UY" "UZ" "CX" "VU" "VA" "GB" "VE" "VN" "TL" "WF" "ZM" "EH" "ZW" )
countries_num=( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
districts=( "CZ0201" "CZ0202" "CZ0641" "CZ0642" "CZ0643" "CZ0801" "CZ0644" "CZ0411" "CZ0422" "CZ0531" "CZ0511" "CZ0311" "CZ0312" "CZ0421" "CZ0321" "CZ0802" "CZ0631" "CZ0100" "CZ0645" "CZ0521" "CZ0512" "CZ0711" "CZ0522" "CZ0632" "CZ0313" "CZ0412" "CZ0803" "CZ0203" "CZ0322" "CZ0204" "CZ0721" "CZ0205" "CZ0513" "CZ0423" "CZ0424" "CZ0206" "CZ0207" "CZ0425" "CZ0523" "CZ0804" "CZ0208" "CZ0712" "CZ0805" "CZ0806" "CZ0532" "CZ0633" "CZ0314" "CZ0324" "CZ0323" "CZ0325" "CZ0315" "CZ0209" "CZ020A" "CZ0713" "CZ0714" "CZ020B" "CZ020C" "CZ0326" "CZ0524" "CZ0514" "CZ0413" "CZ0316" "CZ0533" "CZ0715" "CZ0317" "CZ0327" "CZ0426" "CZ0525" "CZ0634" "CZ0722" "CZ0427" "CZ0534" "CZ0723" "CZ0646" "CZ0724" "CZ0647" "CZ0635" "None" )
districts_num=( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
regions=( "CZ010" "CZ020" "CZ031" "CZ032" "CZ041" "CZ042" "CZ051" "CZ052" "CZ053" "CZ063" "CZ064" "CZ071" "CZ072" "CZ080" "None" )
regions_num=( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 )
err_msg=( "" "date:" "age: " "gender: ")
err_code=0
err_num=0
var_filter_a=0
var_filter_a_value=""
var_filter_b=0
var_filter_b_value=""
var_filter_g=0
var_filter_g_value=""
count=1
command=""
date=""
sheet_name=osoby.csv
RED="\033[0;31m"
NOCOLOR="\033[0m"
################################################
############### HELP FUNCTIONS #################
################################################
# Functions will increase number in arrays
add_one_to_ages() {
if [ "$1" == "" ]; then
(( ages[12]++ ))
elif [ "$1" -lt "6" ]; then
(( ages[0]++ ))
elif [ "$1" -lt "16" ]; then
(( ages[1]++ ))
elif [ "$1" -lt "26" ]; then
(( ages[2]++ ))
elif [ "$1" -lt "36" ]; then
(( ages[3]++ ))
elif [ "$1" -lt "46" ]; then
(( ages[4]++ ))
elif [ "$1" -lt "56" ]; then
(( ages[5]++ ))
elif [ "$1" -lt "66" ]; then
(( ages[6]++ ))
elif [ "$1" -lt "76" ]; then
(( ages[7]++ ))
elif [ "$1" -lt "86" ]; then
(( ages[8]++ ))
elif [ "$1" -lt "96" ]; then
(( ages[9]++ ))
elif [ "$1" -lt "106" ]; then
(( ages[10]++ ))
elif [ "$1" != "" ]; then
(( ages[11]++ ))
fi
}
add_one_to_gender() {
if [ "$1" == "M" ]; then
(( genders[0]++ ))
elif [ "$1" == "Z" ]; then
(( genders[1]++ ))
else
(( genders[2]++ ))
fi
}
add_one_to_country() {
if [ "${1}" != "" ]; then
tmp_counter=-1 # counter for countries
for i in "${countries[@]}"; do
(( tmp_counter++ ))
if [ "$i" == "${1}" ]; then
(( countries_num[$tmp_counter]++ ))
return
fi
done
fi
}
add_one_to_districts() {
if [ "${1}" != "" ]; then
tmp_counter=-1 # counter =
for i in "${districts[@]}"; do
(( tmp_counter++ ))
if [ "$i" == "${1}" ]; then
(( districts_num[$tmp_counter]++ ))
return
fi
done
else
(( districts_num[77]++ ))
fi
}
add_one_to_regions() {
if [ "${1}" != "" ]; then
tmp_counter=-1 # counter
for i in "${regions[@]}"; do
(( tmp_counter++ ))
if [ "$i" == "${1}" ]; then
(( regions_num[$tmp_counter]++ ))
return
fi
done
else
(( regions_num[14]++ ))
fi
}
# Count dates and compare them
count_days() {
if [ "$date" == "" ]; then
date=$(date +$1)
fi
if [ "$date" == "$1" ]; then
count=$((count + 1))
else
echo "$date: $count"
count=$((1))
date=$(date +$1)
fi
}
count_months() {
if [ "$date" == "" ]; then
date=$(date +$1)
date=${date::-3} #delete last 3 chars
else
tmp_date=$(date +$1)
tmp_date=${tmp_date::-3} #format $tmp_date
if [ "$date" == "$tmp_date" ]; then
count=$((count + 1))
else
echo "$date: $count"
count=$((1))
date=$(date +$tmp_date)
fi
fi
}
count_years() {
if [ "$date" == "" ]; then
date=$(date +$1)
date=${date::-6} #delete last 6 chars
else
tmp_date=$(date +$1)
tmp_date=${tmp_date::-6} #format $tmp_date
if [ "$date" == "$tmp_date" ]; then
count=$((count + 1))
else
echo "$date: $count"
count=$((1))
date=$(date +$tmp_date)
fi
fi
}
# Sort filter type. $1 is text from terminal, $2 is position.
sort_type_of_filter() {
if [ "$1" == "-a" ]; then
if [[ $3 == [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] ]];then
var_filter_a=$2
var_filter_a_value=$3
else
>&2 echo "${err_msg[1]}"
fi
elif [ "$1" == "-b" ]; then
if [[ $3 == [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] ]];then
var_filter_a=$2
var_filter_a_value=$3
else
>&2 echo "${err_msg[1]}"
fi
elif [ "$1" == "-g" ]; then
if [[ $3 == [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] ]];then
var_filter_a=$2
var_filter_a_value=$3
else
>&2 echo "${err_msg[1]}"
fi
fi
}
# Save filters
save_filters() {
if [ "$1" == "-a" ] || [ "$1" == "-b" ] || [ "$1" == "-g" ]; then
sort_type_of_filter "$1" "1" "$4"
fi
if [ "$2" == "-a" ] || [ "$2" == "-b" ] || [ "$2" == "-g" ]; then
sort_type_of_filter "$2" "3" "$5"
fi
if [ "$5" == "-a" ] || [ "$5" == "-b" ] || [ "$5" == "-g" ]; then
sort_type_of_filter "$3" "5" "$6"
fi
}
# Save sheet name
save_sheet_name_and_command() {
if [ "$2" == "" ]; then
sheet_name=$1
elif [ "$3" == "" ]; then
sheet_name=$2
command=$1
elif [ "$5" == "" ]; then
sheet_name=$4
command=$3
elif [ "$7" == "" ]; then
sheet_name=$6
command=$5
elif [ "$9" == "" ]; then
sheet_name=$8
command=$7
elif [ "$11" == "" ]; then
sheet_name=$10
command=$9
fi
}
################################################
################# FUNCTIONS ####################
################################################
# Write help text to the terminal.
help() {
printf "${RED}"
echo "--------------------------------------------------"
echo "| |"
echo "| CORONA - POSITIVE USER ANALYZER |"
echo "| |"
echo "--------------------------------------------------"
printf "${NOCOLOR}"
echo "FORMAT:"
echo "corona [-h] [FILTERS] [COMMAND] [LOG [LOG2 [...]]"
echo "--------------------------------------------------"
echo "MAIN:"
echo "-h -> vypise pomocnik"
echo "--------------------------------------------------"
echo "FILTERS:"
echo "-a DATETIME -> vypis zaznamov iba pod danom datume"
echo "-b DATETIME -> vypis zaznamov iba pred danym datumom"
echo "-g GENDER -> filter podla pohlavia (M = muzi, Z = zeny)"
echo "-s [WIDTH] -> vypis dat graficky"
echo "--------------------------------------------------"
echo "COMMAND:"
echo "infected -> spocita pocet nakazenych"
echo "merge -> spoji subory so zaznamami do jedneho"
echo "gender -> pocet nakazenych pre jednotlive pohlavia"
echo "age -> statistika nakazenych podla veku"
echo "daily -> statistika nakazenych pre jednotlive dni"
echo "monthly -> statistika nakazenych pre jednotlive mesiace"
echo "yearly -> statistika nakazenych pre jednotlive roky"
echo "countries -> stastika nakazenych pre krajiny nakazy"
echo "districts -> stastika nakazenych pre okresy"
echo "regions -> stastika nakazenych pre kraje"
printf "${RED}"
echo "--------------------------------------------------"
printf "${NOCOLOR}"
}
# Check if line data are valid, if not, write error
check_data_validity() {
if [[ $1 != [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9] ]];then
err_code=1
elif [[ $2 != [0-1][0-9][0-9] ]] && [[ $2 != [0-9][0-9] ]] && [[ $2 != [0-9] ]]; then
err_code=2
elif [[ $3 != "M" ]] && [[ $3 != "Z" ]] && [[ $3 != "" ]]; then
err_code=3
fi
}
# Return 1 if person go throught all set filters
do_filters() {
if [ "$var_filter_b" != 0 ] && [[ "$var_filter_b_value" > "$datum" ]] && [ "$var_filter_g" != 0 ] && [ "$var_filter_g_value" == "$pohlavi" ]; then
# Set -b and -g
echo "1"
elif [ "$var_filter_a" != 0 ] && [[ "$var_filter_a_value" < "$datum" ]] && [ "$var_filter_g" != 0 ] && [ "$var_filter_g_value" == "$pohlavi" ]; then
# Set -a and -g
echo "1"
elif [ "$var_filter_g" == 0 ] && [ "$var_filter_b" == 0 ] && [ "$var_filter_g" == 0 ]; then
# No filter set
echo "1"
elif [ "$var_filter_g" == 0 ]; then # must be, because it can jump here from second if
if [ "$var_filter_a" != 0 ] && [[ "$var_filter_a_value" < "$datum" ]]; then
# Set just -a
echo "1"
elif [ "$var_filter_b" != 0 ] && [[ "$var_filter_b_value" > "$datum" ]] && [ "$var_filter_g_value" != "$pohlavi" ]; then
# Set just -b
echo "1"
fi
elif [ "$var_filter_g" != 0 ] && [ "$var_filter_g_value" == "$pohlavi" ]; then
# Set just -g
echo "1"
fi
}
# Print results
print_infected() {
total=$(( ${genders[0]} + ${genders[1]} + ${genders[2]} ))
printf "$total\n"
}
print_genders() {
printf "M: ${genders[0]}\n"
printf "Z: ${genders[1]}\n"
if [ "${genders[2]}" != "0" ]; then
printf "None: ${genders[2]}\n"
fi
}
print_age() {
printf "0-5 : ${ages[0]}\n"
printf "6-15 : ${ages[1]}\n"
printf "16-25 : ${ages[2]}\n"
printf "26-35 : ${ages[3]}\n"
printf "36-45 : ${ages[4]}\n"
printf "46-55 : ${ages[5]}\n"
printf "56-65 : ${ages[6]}\n"
printf "66-75 : ${ages[7]}\n"
printf "76-85 : ${ages[8]}\n"
printf "86-95 : ${ages[9]}\n"
printf "96-105: ${ages[10]}\n"
printf ">105 : ${ages[11]}\n"
if [ "${ages[12]}" != "0" ]; then
printf "None : ${ages[12]}\n"
fi
}
print_countries() {
for i in "${!countries[@]}"; do
if [ "${countries_num[$i]}" != "0" ] && [ "${countries[$i]}" != "CZ" ]; then
printf "${countries[$i]}: ${countries_num[$i]}\n"
fi
done
}
print_districts() {
for i in "${!districts[@]}"; do
if [ "${districts_num[$i]}" != "0" ]; then
printf "${districts[$i]}: ${districts_num[$i]}\n"
fi
done
}
print_regions() {
for i in "${!regions[@]}"; do
if [ "${regions_num[$i]}" != "0" ]; then
printf "${regions[$i]}: ${regions_num[$i]}\n"
fi
done
}
print_results() {
if [ "$command" == "infected" ]; then
print_infected
elif [ "$command" == "gender" ]; then
print_genders
elif [ "$command" == "age" ]; then
print_age
elif [ "$command" == "daily" ] || [ "$command" == "monthly" ] ||[ "$command" == "yearly" ]; then
# Print just last line, other lines are printed when program is working
echo "$date: $count"
elif [ "$command" == "countries" ]; then
print_countries
elif [ "$command" == "districts" ]; then
print_districts
elif [ "$command" == "regions" ]; then
print_regions
fi
}
################################################
################### MAIN #######################
################################################
export POSIXLY_CORRECT=yes
if [ "$1" = "-h" ] ; then
help
else
actual_line=0
save_filters "$1" "$3" "$5" "$2" "$4" "$6"
save_sheet_name_and_command "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12"
while IFS=, read -r id datum vek pohlavi kraj_nuts_kod okres_lau_kod nakaza_v_zahranici nakaza_zeme_csu_kod reportovano_khs
do
check_data_validity "$datum" "$vek" "$pohlavi" "$kraj_nuts_kod" "$okres_lau_kod" "$nakaza_v_zahranici" "$nakaza_zeme_csu_kod" "$nakaza_v_zahranici" "$reportovano_khs"
actual_line=$((actual_line + 1))
tmp_filter=`do_filters`
if [ "$actual_line" != "1" ] && [ "$tmp_filter" == "1" ] && [ "$err_code" == 0 ]; then
if [ "$command" == "infected" ]; then
add_one_to_gender "$pohlavi"
elif [ "$command" == "gender" ]; then
add_one_to_gender "$pohlavi"
elif [ "$command" == "age" ]; then
add_one_to_ages "$vek"
elif [ "$command" == "daily" ]; then
count_days "$datum"
elif [ "$command" == "monthly" ]; then
count_months "$datum"
elif [ "$command" == "yearly" ]; then
count_years "$datum"
elif [ "$command" == "countries" ]; then
add_one_to_country "$nakaza_zeme_csu_kod"
elif [ "$command" == "districts" ]; then
add_one_to_districts "$okres_lau_kod"
elif [ "$command" == "regions" ]; then
add_one_to_regions "$kraj_nuts_kod"
else
printf "$id,$datum,$vek,$pohlavi,$kraj_nuts_kod,$okres_lau_kod,$nakaza_v_zahranici,$nakaza_zeme_csu_kod,$reportovano_khs\n"
fi
elif [ "$actual_line" != "1" ] && [ "$err_code" != 0 ]; then
>&2 echo "Invalid ${err_msg[$err_code]} $id,$datum,$vek,$pohlavi,$kraj_nuts_kod,$okres_lau_kod,$nakaza_v_zahranici,$nakaza_zeme_csu_kod,$reportovano_khs"
(( err_num++ ))
fi
err_code=0
done < $sheet_name
print_results
fi
if [ err_num != 0 ]; then
exit 1
else
exit 0
fi