Say we’re down about the lack of sunshine in our city and we really want to know what the sunniest cities in the world are. The data we’re looking at is the Wikipedia article titled List of cities by sunshine duration.

wikipedia screenshot

The general process we follow is 1.) extract the data, 2.) apply various transformations until it’s in the format we want it to be in, then 3.) load into whatever application we want for convenient viewing. As you can imagine, there are many ways to carry out this process, but we’re piping together command line utilities here.

The first step to extracting the text we need is the curl command.

$ curl https://en.wikipedia.org/wiki/List_of_cities_by_sunshine_duration

HTML parsing can be very challenging, so to help narrow down the text we’re most interested in we use the pup utility. Referencing the documentation for this tool we can use the various selectors available to us and grab the text out of the table. The last column in the table is a footnote that we want to leave out.

$ pup '.wikitable:first-of-type td:not(:nth-of-type(16)) text{}'

We now have the text we want, but it’s not quite there yet, pup leaves lots of blank lines in, so to strip those out we use:

$ grep -v '^$'

Next we use awk is an incredibly useful command line tool that is frequently used in Linux systems to pull information out of semi-structured text. awk is well suited to joining each group of 15 lines with commas and a new line at the end to bring back the table cells together to form a CSV.

putting it all together you get:

first try:

curl https://en.wikipedia.org/wiki/List_of_cities_by_sunshine_duration |
pup '.wikitable:first-of-type td:not(:nth-of-type(16)) text{}' | 
grep -v '^$' |
awk '{printf "%s%s", (NR%15 == 1 ? "" : ", "), $0} NR % 15 == 0 {print ""}'

second try:

curl https://en.wikipedia.org/wiki/List_of_cities_by_sunshine_duration |
pup '.wikitable:nth-of-type(4) tr > :not(:last-child) text{}' |
grep -v '^$' |
awk '{printf "%s%s", (NR%15 == 1 ? "" : ", "), $0} NR % 15 == 0 {print ""}'

one row of weird data in North America is messing this up: “Saint Pierre and Miquelon Saint-Pierre” so we need a little refinement to our awk script. Luckily, LLMs are well versed in awk. Here’s the final version of the awk script:

final commands here:

curl https://en.wikipedia.org/wiki/List_of_cities_by_sunshine_duration |
pup '.wikitable:nth-of-type(4) tr > :not(:last-child) text{}' |
grep -v '^$' |
awk -f row-text-to-csv.awk > test.csv

Wow! that turned into more work than I expected. Surely there’s an easier way to do this! Next time. Here’s the data:

Country CityJanFebMarAprMayJunJulAugSepOctNovDecYear
Canada Calgary119.5144.6177.2220.2249.4269.9314.1284.0207.0175.4121.1114.02396.3
Canada Churchill79.7117.7177.8198.2197.0243.0281.7225.9112.058.155.353.11799.5
Canada Edmonton100.8121.7176.3244.2279.9285.9307.5282.3192.7170.898.484.52344.8
Canada Iqaluit32.494.0172.2216.5180.5200.2236.8156.887.951.435.612.61476.8
Canada Montreal101.0128.0164.0178.0229.0240.0272.0246.0182.0144.084.084.02051.0
Canada Prince Rupert40.165.2103.0145.8171.1154.5149.7149.7115.772.443.032.11242.1
Canada Toronto85.9111.3161.0180.0227.7259.6279.6245.6194.4154.388.978.12066.4
Canada Vancouver60.191.0134.8185.0222.5226.9289.8277.1212.8120.760.456.51937.6
Canada Whitehorse43.8105.5163.2238.5251.1266.7247.6226.5132.784.939.826.81827.1
Canada Winnipeg114.7133.9181.9241.4285.3276.3308.3281.4189.0147.493.999.52352.9
Honduras La Ceiba170.5192.1217.0234.0213.9192.0201.5217.0174.0151.9144.0151.92259.8
Mexico La Paz2002342712923323222872582572722331903148
Mexico Mexico City2402342682322251831761761571942322362555
Mexico Monterrey1421541951931922062492422001701631332239
Mexico Villahermosa1681852522152782172522451671501701442443
Nicaragua Managua263.5254.2291.4276.0229.4186.0151.9195.3210.0223.2231.0248.02759.9
Panama Panama City228.9245.2183.9173.1108.5116.3106.1118.199.2103.9139.8120.51743.5
Puerto Rico San Juan237.4231.2282.0268.3255.2259.4280.8267.8234.7227.2202.4217.42963.8
El Salvador San Salvador3012772942432201742392571802112672942957
Saint Pierre and Miquelon Saint-Pierre49.670.2115.5131.9165.8172.6164.8173.5156.1119.063.045.41427.3
United States Albuquerque234.2225.3270.2304.6347.4359.3335.0314.2286.7281.4233.8223.33415.4
United States Anchorage82.9120.5195.8235.3288.7274.7250.1203.9159.8117.180.651.82061.2
United States Atlanta164.0171.7220.5261.2288.6284.8273.8258.6227.5238.5185.1164.02738.3
United States Austin163.8169.3205.9205.8227.1285.5317.2297.9233.8215.6168.3153.52643.7
United States Baltimore155.4164.0215.0230.7254.5277.3290.1264.4221.8205.5158.5144.52581.7
United States Boise109.3151.9238.6281.4335.5351.6399.8358.8303.6238.1119.6105.22993.4
United States Boston163.4168.4213.7227.2267.3286.5300.9277.3237.1206.3143.2142.32633.6
United States Charlotte173.3180.3234.8269.6292.1289.2290.0272.9241.4230.5178.4168.52821.0
United States Chicago135.8136.2187.0215.3281.9311.4318.4283.0226.6193.2113.3106.32508.4
United States Cleveland101.0122.3167.0216.0263.6294.6307.2262.2219.0169.589.867.82280.0
United States Columbus110.6126.3162.0201.8243.4258.1260.9235.9212.0183.1104.284.32182.6
United States Dallas183.5178.3227.7236.0258.4297.8332.4304.5246.2228.1183.8173.02849.7
United States Denver215.3211.1255.6276.2290.0315.3325.0306.4272.3249.2194.3195.93106.6
United States Detroit119.9138.3184.9217.0275.9301.8317.0283.5227.6176.0106.387.72435.9
United States El Paso254.5263.0326.0348.0384.7384.1360.2335.4304.1298.6257.6246.33762.5
United States Fresno141.5196.9286.2335.5398.9412.2428.2399.6345.9302.3189.9127.13564.2
United States Honolulu213.5212.7259.2251.8280.6286.1306.2303.1278.8244.0200.4199.53035.9
United States Houston143.4155.0192.5209.8249.2281.3293.9270.5236.5228.8168.3148.72577.9
United States Indianapolis132.1145.7178.3214.8264.7287.2295.2273.7232.6196.6117.1102.42440.4
United States Jacksonville189.4193.8257.9286.4303.9283.6282.0262.4228.2214.6193.9183.62879.7
United States Kansas City183.7174.3223.9257.8285.0305.5329.3293.9240.5213.6155.3147.12809.9
United States Las Vegas245.2246.7314.6346.1388.1401.7390.9368.5337.1304.4246.0236.03825.3
United States Los Angeles225.3222.5267.0303.5276.2275.8364.1349.5278.5255.1217.3219.43254.2
United States Louisville140.5148.9188.6221.1263.4288.9293.6272.6234.3208.5135.7118.32514.4
United States Memphis166.6173.8215.3254.6301.5320.6326.9307.0251.2245.9173.0151.92888.3
United States Miami219.8216.9277.2293.8301.3288.7308.7288.3262.2260.2220.8216.13154.0
United States Milwaukee140.2151.5185.4213.5275.5304.5321.1281.2215.1178.0112.8104.82483.6
United States Minneapolis156.7178.3217.5242.1295.2321.9350.5307.2233.2181.0112.8114.32710.7
United States Nashville139.6145.2191.3231.5261.8277.7279.0262.1226.4216.8148.1130.62510.1
United States New Orleans153.0161.5219.4251.9278.9274.3257.1251.9228.7242.6171.8157.82648.9
United States New York City162.7163.1212.5225.6256.6257.3268.2268.2219.3211.2151.0139.02534.7
United States Nome62.2140.1205.0245.3290.3275.3250.3178.1153.6116.766.453.02036.3
United States Oklahoma City200.8189.7244.2271.3295.2326.1356.6329.3263.7245.1186.5180.93089.4
United States Omaha167.8157.6206.4230.1277.1314.0332.5296.3245.5217.5148.0134.12726.9
United States Philadelphia155.7154.7202.8217.0245.1271.2275.6260.1219.3204.5154.7137.72498.4
United States Phoenix256.0257.2318.4353.6401.0407.8378.5360.8328.6308.9256.0244.83871.6
United States Pittsburgh93.9108.5155.4182.8217.4242.2254.9228.4196.7167.399.474.42021.3
United States Portland (OR)85.6116.4191.1221.1276.1290.2331.9298.1235.7151.779.363.72340.9
United States Raleigh163.8173.1228.9250.7258.4267.7259.5239.6217.6215.4174.0157.62606.3
United States Richmond (VA)172.5179.7233.3261.6288.0306.4301.4278.9237.9222.8183.5163.02829.0
United States Sacramento145.5201.3278.0329.6406.3419.5440.2406.9347.8296.7194.9141.13607.8
United States Salt Lake City127.4163.1241.9269.1321.7360.5380.5352.5301.1248.1150.4113.13029.4
United States San Antonio159.4169.7215.5209.7221.8275.9308.8293.9234.9218.0171.9149.72629.2
United States San Diego239.3227.4261.0276.2250.5242.4304.7295.0253.3243.4230.1231.33054.6
United States San Francisco185.9207.7269.1309.3325.1311.4313.3287.4271.4247.1173.4160.63061.7
United States Seattle69.8108.8178.4207.3253.7268.4312.0281.4221.7142.672.752.92169.7
United States St. Louis161.2158.3198.3223.5266.5291.9308.9269.8236.1208.4140.9129.92593.7
United States Tampa199.9202.7267.5299.1314.5277.8265.3249.5223.0233.9201.7191.62926.5
United States Tucson259.9258.2320.7357.2400.8396.9342.7335.6316.4307.4264.4245.83806.0
United States Tulsa175.8171.7219.6244.4266.7294.8334.7305.3232.5218.6161.1160.82786.0
United States Virginia Beach171.5175.2229.3252.8271.7280.1278.3260.4231.4208.3175.7160.42695.1
United States Washington D.C.144.6151.8204.0228.2260.5283.2280.5263.1225.0203.6150.2133.02527.7
United States Wichita190.9186.4230.4257.8289.8305.0342.1309.2245.6226.3170.2168.72922.4
United States Yuma268.4270.8335.5365.5407.4415.4392.6375.6341.7319.6270.1252.74015.3