-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-dr5hn.sh
executable file
·63 lines (54 loc) · 1.71 KB
/
process-dr5hn.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
#!/bin/bash
# Define the URL of the CSV file
URL="https://github.com/dr5hn/countries-states-cities-database/raw/master/csv/cities.csv"
# Define the name of the local file and the directory to save the filtered file
FILE="cities.csv"
OUTPUT_DIR="generated_data"
OUTPUT_FILE="${OUTPUT_DIR}/turkish_cities.csv"
TEMP_FILE="temp_filtered_cities.csv"
SUMMARY_FILE="temp_summary.csv"
# Create the output directory if it doesn't exist
mkdir -p $OUTPUT_DIR
# Download the CSV file
# if you don't want to download the file, comment out below line
curl -L $URL -o $FILE
# Process the file to filter and replace "Merkez" with state name
awk -F',' 'BEGIN {OFS=","}
NR==1 {next}
$7 == "TR" {
name = $2 == "Merkez" ? $5 : $2
print name, $5, tolower($7), $9, $10
}' $FILE | sed 's/â/a/g' >$TEMP_FILE
# Generate state-wise summaries if the state name is not present as a city name
awk -F',' 'BEGIN {OFS=","}
NR == 1 {next}
{
state = $2
if ($1 == state) {
states[state] = 1
}
lat[state] += $4
lon[state] += $5
count[state]++
data[state] = $2
}
END {
for (s in data) {
if (!(s in states)) {
avg_lat = lat[s] / count[s]
avg_lon = lon[s] / count[s]
print s, s, "tr", avg_lat, avg_lon
}
}
}
' $TEMP_FILE >$SUMMARY_FILE
# Combine the header, summary, and filtered data into the final output file
{
echo "name,state_name,country_code,latitude,longitude"
cat $SUMMARY_FILE
cat $TEMP_FILE
} >$OUTPUT_FILE
# Notify the user
echo "Processing complete. Filtered data saved in '$OUTPUT_FILE'."
# Clean up temporary files
rm $TEMP_FILE $SUMMARY_FILE