On Technology
Published in

On Technology

Command Line Analytics

Analysing Election Results like a boss

Basic Commands

cat presidential_election_results.denorm.csv
year,result_code,electoral_district_name,polling_division_name,party,candidate,votes,percentage,affiliation1982,01A,Colombo,Colombo North,UNP,Junius Richard Jayewardene,29380,69.03,green1982,01A,Colombo,Colombo North,SLFP,Hector Kobbekaduwa,10641,25.0,blue1982,01A,Colombo,Colombo North,JVP,Rohana Wijeweera,1645,3.86,red1982,01A,Colombo,Colombo North,LSSP,Colvin Reginald de Silva,361,0.85,red1982,01A,Colombo,Colombo North,ACTC,Kumar Ponnambalam,477,1.12,yellow1982,01A,Colombo,Colombo North,NSSP,Vasudeva Nanayakkara,60,0.14,red1988,01A,Colombo,Colombo North,UNP,Ranasinghe Premadasa,22389,63.26,green1988,01A,Colombo,Colombo North,SLFP,Sirimavo Bandaranaike,10744,30.36,blue1988,01A,Colombo,Colombo North,SLPP,Oswin Abeygunasekara,2259,6.38,purple...
head -n 1 presidential_election_results.denorm.csv
year,result_code,electoral_district_name,polling_division_name,party,candidate,votes,percentage,affiliation
head -n 2 presidential_election_results.denorm.csvyear,result_code,electoral_district_name,polling_division_name,party,candidate,votes,percentage,affiliation1982,01A,Colombo,Colombo North,UNP,Junius Richard Jayewardene,29380,69.03,green
head -n 2 presidential_election_results.denorm.csv | tr , ‘\t’year result_code electoral_district_name polling_division_name party candidate votes percentage affiliation1982 01A Colombo Colombo North UNP Junius Richard Jayewardene 29380 69.03 green
head -n 2 presidential_election_results.denorm.csv | cut -d, -f5,6,7 | tr , '\t'party candidate votesUNP Junius Richard Jayewardene 29380
cat  presidential_election_results.denorm.csv | grep 1982 | grep "Colombo North"  | cut -d, -f5,6,7 | tr , '\t'UNP Junius Richard Jayewardene 29380SLFP Hector Kobbekaduwa 10641JVP Rohana Wijeweera 1645LSSP Colvin Reginald de Silva 361ACTC Kumar Ponnambalam 477NSSP Vasudeva Nanayakkara 60
cat  presidential_election_results.denorm.csv | grep 1982 | grep "Colombo North"  | sort -t, -nr -k7 | cut -d, -f5,6,7 | tr , '\t'UNP Junius Richard Jayewardene 29380SLFP Hector Kobbekaduwa 10641JVP Rohana Wijeweera 1645ACTC Kumar Ponnambalam 477LSSP Colvin Reginald de Silva 361NSSP Vasudeva Nanayakkara 60
cat  presidential_election_results.denorm.csv | grep 1982 | grep "Colombo North"  | wc -l6
cat  presidential_election_results.denorm.csv | awk '!/Final Results|AICUM|Postal/' | grep 1982  | grep SLFP | cut -d, -f4,7,8 | tr , '\t' | sortAgalawatta 21041 41.25Akmeemana 19623 46.97Akuressa 20389 40.57Ambalangoda 16225 43.99Ampara 20552 38.71Anamaduwa 19757 45.71Anuradhapura-East 14944 45.31Anuradhapura-West 15054 42.19Aranayake 10433 35.95Attanagalla 31495 55.92...

Answering questions like a Nerd

How many candidates have ever contested the presidential election? Who are they?

awk 'NR > 1' presidential_election_results.denorm.csv | cut -d, -f6 | sort | uniq | wc -l64
awk 'NR > 1' presidential_election_results.denorm.csv | cut -d, -f6 | sort | uniqA. A. SuraweeraA. DissanayakaA. K. J. ArachchigeA. S. P. LiyanageA. W. PremawardhanaAbdul RasoolAithurus Mohamed Illias

Who are the Rajapaksa’s who have contested the Presidential Election? Sirisenas? Sinhes?

awk 'NR > 1' presidential_election_results.denorm.csv | cut -d, -f6 | sort | uniq | grep RajapaksaMahinda RajapaksaNamal Ajith Rajapaksaawk 'NR > 1' presidential_election_results.denorm.csv | cut -d, -f6 | sort | uniq | grep SirisenaArachchige Rathnayaka SirisenaMaithripala Sirisenaawk 'NR > 1' presidential_election_results.denorm.csv | cut -d, -f6 | sort | uniq | grep sinArthur Jayasena RanasingheHudson SamarasingheRajiva WijesinhaRanasinghe PremadasaRanil Wickremesinghe

What parties have contested presidential elections? Any with SL?

awk 'NR > 1' presidential_election_results.denorm.csv | cut -d, -f5 | sort | uniqAACAAKOACAKOACTCDNMDUADUNFFSPawk 'NR > 1' presidential_election_results.denorm.csv | cut -d, -f5 | sort | uniq | grep SLSLFPSLLPSLMPSLNFSLPFSLPP

What is the highest percentage of votes a candidate has won in a polling division? Who and when?

cut -d, -f1,4,6,7,8 presidential_election_results.denorm.csv | awk '!/Postal|Final/'  | sort -t, -nr -k5 | head -n 5 | tr , '\t'2005 Kilinochchi Ranil Wickremesinghe 1 100.01994 Point Pedro Chandrika Kumaratunga 8 100.01994 Manipay Chandrika Kumaratunga 18 100.01994 Kayts Chandrika Kumaratunga 14761 97.162005 Paddiruppu Ranil Wickremesinghe 24142 94.72

Who won Borella in 2005?

grep Borella presidential_election_results.denorm.csv | grep 2005 | cut -d, -f5,6,7,8 | sort -t, -nr -k3 | tr , '\t'UNP Ranil Wickremesinghe 25784 61.67UPFA Mahinda Rajapaksa 15644 37.42USP Siritunga Jayasuriya 115 0.28ULPP Victor Hettigoda 80 0.19JSP A. A. Suraweera 60 0.14NLF Chamil Jayaneththi 32 0.08SEP Wije Dias 23 0.06ULF Anura De Silva 22 0.05RPP Aruna de Soyza 18 0.04SLNF Wimal Geeganage 17 0.04DUA A. K. J. Arachchige 10 0.02SLPF P. Nelson Perera 3 0.01UNAF H. S. Dharmadwaja 1 0.0

Who has historically won Attanagalla?

grep Attanagalla presidential_election_results.denorm.csv | sort -t, -nr -k8  | head -n 7 | sort | cut -f 1,5,6,7,8 -d, | tr , '\t'1982 SLFP Hector Kobbekaduwa 31495 55.921988 SLFP Sirimavo Bandaranaike 35758 60.121994 PA Chandrika Kumaratunga 51923 73.641999 PA Chandrika Kumaratunga 51204 65.792005 UPFA Mahinda Rajapaksa 52393 60.522010 UPFA Mahinda Rajapaksa 61337 66.32015 UPFA Mahinda Rajapaksa 54777 51.71

What is the highest number of votes a candidate has won in a polling division? Which year was that?

cat  presidential_election_results.denorm.csv | awk '!/Final Results|AICUM|Postal/' | sort -t, -nr -k 7 | head -n 5 | cut -d, -f1,4,6,7 | tr , '\t'2015 Nuwara Eliya-Maskeliya Maithripala Sirisena 1715782005 Nuwara Eliya-Maskeliya Ranil Wickremesinghe 1563432010 Nuwara Eliya-Maskeliya Sarath Fonseka 1128662015 Batticaloa Maithripala Sirisena 977791994 Nuwara Eliya-Maskeliya Chandrika Kumaratunga 94262

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nuwan I. Senaratna

I am a Computer Scientist and Musician by training. A writer with interests in Philosophy, Economics, Technology, Politics, Business, the Arts and Fiction.