This repository was archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtallymobility.sh
executable file
·81 lines (68 loc) · 1.75 KB
/
tallymobility.sh
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
#!/bin/bash
#
# Mobility data have the following attributes:
# id_origin,id_destination,journeys,people
#
# This script searches in a given directory for all the files matching the
# pattern YYYY-MM-DD.csv (mobility data), read the file contents and append
# the total number of journeys and people to the name of the file.
#
# YYYY-MM-DD.csv -> YYYY-MM-DD^JOURNEYS-PEOPLE.csv
#
# color codes
orange=202
red=160
blue=33
green=34
# color echo message
function colored_echo {
case $# in
# just the message
1) msg=$1
;;
# message with foreground color
2) msg="\\033[38;5;${2}m${1}\\033[0m"
;;
# message with foreground and background color
*) msg="\\033[48;5;${3};38;5;${2}m${1}\\033[0m"
;;
esac
echo -e "$msg"
}
# verify input
if [ $# -ne 1 ];
then
colored_echo "Use: $0 <directory>" $red
exit 1
fi
# directory where are the files
directory="$1"
# file pattern to match
file_pattern="^([0-9]{4}-[0-9]{2}-[0-9]{2})\.csv$"
# old working directory
old_wd=`pwd`
colored_echo "Searching files in $1" $green
# enter in directory
cd $directory
# get all .csv files in current directory
for file in `ls *.csv`; do
# if the current file matches our pattern
if [[ $file =~ $file_pattern ]];
then
# process it
sum=(`awk -F, '{ journeys_sum += $3; people_sum += $4 } END { print journeys_sum, people_sum }' $file`)
# new file name
new_file="${BASH_REMATCH[1]}^${sum[0]}-${sum[1]}.csv"
# rename file
mv $file $new_file
# show processed information
colored_echo "old file: ${file}, journeys: ${sum[0]}, people: ${sum[1]}, new file: ${new_file}" $green
else
# ignore it
colored_echo "Ignoring: $file" $orange
fi
done
# return to previous working directory
cd $old_wd
colored_echo "Done!" $green
exit 0