#!/bin/bash
# TASK for a purely-commandline job:
# determine the total population of the 50 largest cities in Scotland
# See Wikipedia page on "List of towns and cities in Scotland by population"
# URL: https://en.wikipedia.org/wiki/List_of_towns_and_cities_in_Scotland_by_population
# see clean_cities.txt for development steps of this script
# -------------------------------------------------------

cat ScotCities.txt | sed -e 's/\([0-9]*\)\([ \t]*\)\([a-zA-Z]*\)[ ]\([a-zA-Z]*\)\([ \t]*\)\([0-9]*\),/\1\2\3_\4\5\6/g' | sed -e 's/\([a-zA-Z]*\)_\([ \t]\)/\1\2/g' | sed -e '1d' | gawk -e '{ print "{"; print "rank: "; print $1; print ","; print "name: " ; print $2; print ","; print "pop: " ; print $3; print ","; print "status:" ; print $4; print "," ; print "council: "; print $5 $6 ; print "}" }' | sed ':a;N;$!ba;s/\n/ /g' | sed -e 's/} {/} , {/g' | sed -e 's/\([a-z]*\):/"\1":/g' | sed -e 's/\([0-9]*,[0-9]*\)[ ]*,/"\1" ,/g' | sed -e 's/:[ ]*\([a-zA-Z_]*\)[ ]*\([,;}]\)/: "\1" \2/g'  > q2.json ; echo "[ " >> q0.json ; cat q2.json  >> q0.json ; echo "]" >> q0.json; mv q0.json qq.json ; python3 -c 'import itertools; import functools; import json; jfile = "qq.json"; city_sorted = json.loads(open(jfile,"r").read()) ; city_sorted.sort(key=lambda c: c["rank"]) ; s = functools.reduce( lambda s, x: s + x, map ( lambda c: int(c["pop"]),  city_sorted) );  print("The total population of the 50 largest cities/towns is: " + str(s)) '
