-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathiso3166-1alpha2-to-sql
executable file
·42 lines (34 loc) · 1.06 KB
/
iso3166-1alpha2-to-sql
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
#!/bin/bash
# Convert a list of country codes
# in ISO3166-1 alpha 2 format to a SQL script.
#
# 2010, Mathieu Comandon <[email protected]>
#
# This file is under a WTFPL Licence
#
# The country code list is located at :
# http://www.iso.org/iso/iso3166_en_code_lists.txt
# For more information about ISO 3166-1 visit Wikipedia :
# http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
set -e
text_file=$1
if [ ! -f $text_file ]; then
text_file = "/tmp/iso3166_en_code_lists.txt"
wget http://www.iso.org/iso/iso3166_en_code_lists.txt -O $text_file
fi
table='country_code'
IFS=$(echo -en "\n\b")
dest="$HOME/isocodes.sql"
rm $dest
for line in $(cat "$text_file") ; do
len=${#line}
if [ $len -lt 200 -a $len -gt 4 ] ; then
new_line=$(echo $line | iconv -f iso-8859-1 -t utf-8 -)
country=${new_line%;*}
country=$(echo $country | sed -e "s/'/\\\\'/")
code=${new_line#*;}
code=$(echo -n $code | tr -d '\r')
echo "INSERT INTO ${table} (country, code) VALUES (\"${country}\", \"${code}\");" >> $dest
fi
done
unset IFS