skip to Main Content

I’m trying to write a code that lists out the names of the countries reported by
2022 World Happiness Report ranked by the happiness index, given in HTML format via this link: https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw and my response is supposed to be:

Finland
Norway
Denmark
Iceland
Switzerland
Netherlands

I put my code into https://www.shellcheck.net/ and it says there is a problem with the ‘rankings’ line in my code

#!/bin/bash

data=$(curl -s https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw)
content=$(echo "$data" | awk '/=== 2022 report ===/,/=== 2021 report ===/' | sed -e 's/|[1-5]||{{flag|//g' -e 's/}}//g')
rankings=$(echo "$content" | awk '/^[^[]/ && /^[^=]/' | sort -nr -k1 | head -5 | sed -e 's/-//g' -e 's/|//g')
echo "$rankings" | tr -s 'n'

The problem is I’m not getting a response, literally, it looks like this:

XXXXXXXX@LAPTOP:~# ./d2.sh    //d2 is the name of the file (I have already turned it into an executable using chmod +x)

XXXXXXXX@LAPTOP:~#

can anyone please help explain what should I change to get a response like the one I expected to get?

3

Answers


  1. Your sed commands have a space before {{flag.
    When I tried running your commands, there were no spaces before the curly brackets in the output.

    Removing the spaces in the command almost works, some countries have some extra data on their line. You can trim this by adding for example a cut. So your final line would be:

    filtered_content=$(echo "$content" | sed -n '/^|{{flag|/p' | sed -e 's/^|{{flag|//g' -e 's/}}//g' | cut -d '|' -f1)"
    
    Login or Signup to reply.
  2. this doesn’t solve everything, but at least it’s down to just the list of nations : (unfortunately I couldn’t get it to work without that extra gcat -)

    curl -s -L 'https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw' | 
    
    gcat - | 
    
    mawk2 '/^[|].+[{]+[Ff]lag[|][^}]+[}][}][*]*$|=== (19|20)[0-9][0-9] [^=]+ ===/'
    

         1  === 2023 World Happiness Report ===
         2  === 2022 World Happiness Report ===
         3  === 2021 World Happiness Report ===
         4  === 2020 World Happiness Report ===
         5  === 2019 World Happiness Report ===
         6  === 2018 World Happiness Report ===
         7  === 2017 World Happiness Report ===
         8  === 2016 World Happiness Report ===
         9  === 2015 World Happiness Report ===
        10  === 2013 World Happiness Report ===
        11  === 2012 World Happiness Report ===
        12  === 2023 report ===
        13  |1||{{flag|Finland}}
        14  |2||{{flag|Denmark}}
        15  |3||{{flag|Iceland}}
        16  |4||{{flag|Israel}}
        17  |5||{{flag|Netherlands}}
        18  |6||{{flag|Sweden}}
        19  |7||{{flag|Norway}}
        20  |8||{{flag|Switzerland}}
        21  |9||{{flag|Luxembourg}}
        22  |10||{{flag|New Zealand}}
        23  |11||{{flag|Austria}}
        24  |12||{{flag|Australia}}
        25  |13||{{Flag|Canada}}
        26  |14||{{Flag|Ireland}}
        27  |15||{{flag|United States}}
        28  |16||{{flag|Germany}}
        29  |17||{{flag|Belgium}}
        30  |18||{{flag|Czech Republic}}
        31  |19||{{flag|United Kingdom}}
        32  |20||{{flag|Lithuania}}
        33  |21||{{flag|France}}
        34  |22||{{flag|Slovenia}}
        35  |23||{{flag|Costa Rica}}
        36  |24||{{flag|Romania}}
        37  |25||{{flag|Singapore}}*
        38  |26||{{flag|United Arab Emirates}}
        39  |27||{{flag|Taiwan}}
        40  |28||{{flag|Uruguay}}
        41  |29||{{flag|Slovakia}}*
        42  |30||{{flag|Saudi Arabia}}
        43  |31||{{flag|Estonia}}
        44  |32||{{flag|Spain}}
        45  |33||{{flag|Italy}}
        46  |34||{{flag|Kosovo}}
        47  |35||{{flag|Chile}}
        48  |36||{{flag|Mexico}}
        49  |37||{{flag|Malta}}
        50  |38||{{flag|Panama}}
        51  |39||{{flag|Poland}}
        52  |40||{{flag|Nicaragua}}
        53  |41||{{flag|Latvia}}
        54  |42||{{flag|Bahrain}}*
        55  |43||{{flag|Guatemala}}
        56  |44||{{flag|Kazakhstan}}
        57  |45||{{flag|Serbia}}*
        58  |46||{{flag|Cyprus}}
        59  |47||{{flag|Japan}}
        60  |48||{{flag|Croatia}}
        61  |49||{{flag|Brazil}}
        62  |50||{{flag|El Salvador}}
        63  |51||{{flag|Hungary}}
        64  |52||{{flag|Argentina}}
        65  |53||{{flag|Honduras}}
        66  |54||{{flag|Uzbekistan}}
        67  |55||{{flag|Malaysia}}*
        68  |56||{{flag|Portugal}}
        69  |57||{{flag|South Korea}}
        70  |58||{{flag|Greece}}
        71  |59||{{flag|Mauritius}}
        72  |60||{{flag|Thailand}}
        73  |61||{{flag|Mongolia}}
        74  |62||{{flag|Kyrgyzstan}}
        75  |63||{{flag|Moldova}}
        76  |64||{{flag|China}}*
        77  |65||{{flag|Vietnam}}
        78  |66||{{flag|Paraguay}}
        79  |67||{{flag|Montenegro}}*
        80  |68||{{flag|Jamaica}}
        81  |69||{{flag|Bolivia}}
        82  |70||{{flag|Russia}}
        83  |71||{{flag|Bosnia and Herzegovina}}*
        84  |72||{{flag|Colombia}}
        85  |73||{{flag|Dominican Republic}}
        86  |74||{{flag|Ecuador}}
        87  |75||{{flag|Peru}}
        88  |76||{{flag|Philippines}}*
        89  |77||{{flag|Bulgaria}}
        90  |78||{{flag|Nepal}}
        91  |79||{{flag|Armenia}}
        92  |80||{{flag|Tajikistan}}*
        93  |81||{{flag|Algeria}}*
        94  |82||{{flag|Hong Kong}}
        95  |83||{{flag|Albania}}
        96  |84||{{flag|Indonesia}}
        97  |85||{{flag|South Africa}}*
        98  |86||{{flag|Congo, Republic of}}
        99  |87||{{flag|North Macedonia}}
       100  |88||{{flag|Venezuela}}
       101  |89||{{flag|Laos}}*
       102  |90||{{flag|Georgia}}
       103  |91||{{flag|Guinea}}
       104  |92||{{flag|Ukraine}}
       105  |93||{{flag|Ivory Coast}}
       106  |94||{{flag|Gabon}}
       107  |95||{{flag|Nigeria}}*
       108  |96||{{flag|Cameroon}}
       109  |97||{{flag|Mozambique}}
       110  |98||{{flag|Iraq}}*
       111  |99||{{flag|Palestine}}
       112  |100||{{flag|Morocco}}
       113  |101||{{flag|Iran}}
       114  |102||{{flag|Senegal}}
       115  |103||{{flag|Mauritania}}
       116  |104||{{flag|Burkina Faso}}*
       117  |105||{{flag|Namibia}}
       118  |106||{{flag|Turkey}}*
       119  |107||{{flag|Ghana}}
       120  |108||{{flag|Pakistan}}
       121  |109||{{flag|Niger}}
       122  |110||{{flag|Tunisia}}
       123  |111||{{flag|Kenya}}
       124  |112||{{flag|Sri Lanka}}*
       125  |113||{{flag|Uganda}}*
       126  |114||{{flag|Chad}}
       127  |115||{{flag|Cambodia}}
       128  |116||{{flag|Benin}}
       129  |117||{{flag|Myanmar}}*
       130  |118||{{flag|Bangladesh}}
       131  |119||{{flag|Gambia}}
       132  |120||{{flag|Mali}}
       133  |121||{{flag|Egypt}}
       134  |122||{{flag|Togo}}
       135  |123||{{flag|Jordan}}
       136  |124||{{flag|Ethiopia}}
       137  |125||{{flag|Liberia}}
       138  |126||{{flag|India}}
       139  |127||{{flag|Madagascar}}
       140  |128||{{flag|Zambia}}*
       141  |129||{{flag|Tanzania}}
       142  |130||{{flag|Comoros}}
       143  |131||{{flag|Malawi}}
       144  |132||{{flag|Botswana}}
       145  |133||{{flag|Congo, Democratic Republic of}}
       146  |134||{{flag|Zimbabwe}}
       147  |135||{{flag|Sierra Leone}}
       148  |136||{{flag|Lebanon}}
       149  |137||{{flag|Afghanistan}}
       150  === 2022 report ===
       151  |1||{{flag|Finland}}
       152  |2||{{flag|Denmark}}
       153  |3||{{flag|Iceland}}
       154  |4||{{flag|Switzerland}}
       155  |5||{{flag|Netherlands}}
       156  |6||{{flag|Luxembourg}}
       157  |7||{{flag|Sweden}}
       158  |8||{{flag|Norway}}
       159  |9||{{flag|Israel}}
       160  |10||{{flag|New Zealand}}
       161  |11||{{flag|Austria}}
       162  |12||{{flag|Australia}}
       163  |13||{{Flag|Ireland}}
       164  |14||{{Flag|Germany}}
       165  |15||{{flag|Canada}}
       166  |16||{{flag|United States}}
       167  |17||{{flag|United Kingdom}}
       168  |18||{{flag|Czech Republic}}
       169  |19||{{flag|Belgium}}
       170  |20||{{flag|France}}
       171  === 2020 report ===
       172  |{{flag|Uruguay}}
       173  |{{flag|Saudi Arabia}}
       174  |{{flag|Spain}}
       175  |{{flag|Guatemala}}
       176  |{{flag|Italy}}
       177  |{{flag|Singapore}}
       178  |{{flag|Brazil}}
       179  |{{Flag|Poland}}
       180  |{{Flag|Colombia}}
       181  |{{Flag|Cyprus}}
       182  |{{Flag|Nicaragua}}
       183  |{{Flag|Romania}}
       184  |{{Flag|Kuwait}}
       185  |{{Flag|Mauritius}}
       186  |{{Flag|Kazakhstan}}
       187  |{{Flag|Estonia}}
       188  |{{flag|Jamaica}}
       189  |{{flag|South Korea}}
       190  |{{flag|Japan}}
       191  |{{flag|Peru}}
       192  |{{flag|Serbia}}
       193  |{{flag|Bolivia}}
       194  |{{Flag|Pakistan}}
       195  |{{Flag|Paraguay}}
       196  |{{flag|Dominican Republic}}
       197  |{{flag|Bosnia and Herzegovina}}
       198  |{{flag|Moldova}}
       199  |{{flag|Tajikistan}}
       200  |{{flag|Montenegro}}
       201  |{{flag|Russia}}
       202  |{{flag|Kyrgyzstan}}
       203  |{{flag|Belarus}}
       204  |{{flag|North Cyprus}}
       205  |{{flag|Greece}}
       206  |{{flag|Hong Kong}}
       207  |{{flag|Croatia}}
       208  |{{flag|Gabon}}
       209  |{{flag|South Africa}}
       210  |{{flag|Iraq}}
       211  |{{flag|Lebanon}}
       212  |{{flag|Burkina Faso}}
       213  |{{flag|Gambia}}
       214  |{{flag|Mali}}
       215  |{{flag|Nigeria}}
       216  |{{flag|Eswatini}}
       217  |{{flag|Myanmar}}
       218  |{{flag|Comoros}}
       219  |{{flag|Togo}}
       220  |{{flag|Ethiopia}}
       221  |{{flag|Madagascar}}
       222  |{{flag|Egypt}}
       223  |{{flag|Sierra Leone}}
       224  |{{flag|Burundi}}
       225  |{{flag|Zambia}}
       226  |{{flag|Haiti}}
       227  |{{flag|Lesotho}}
       228  |{{flag|India}}
       229  |{{flag|Malawi}}
       230  |{{flag|Yemen}}
       231  |{{flag|Botswana}}
       232  |{{flag|Tanzania}}
       233  |{{flag|Central African Republic}}
       234  |{{flag|Rwanda}}
       235  |{{flag|Zimbabwe}}
       236  |{{flag|South Sudan}}
       237  |{{flag|Afghanistan}}
       238  === 2019 report ===
       239  === 2013 report ===
    
    Login or Signup to reply.
  3. I’m not 100% certain I understand what you’re trying to achieve, especially with the sort, the way I understand it that list already comes sorted from "happiest" to "least happy".

    You also don’t seem to have a good handle on what your data actually looks like – you’re trying to define an awk-range, but the string === 2021 report === doesn’t feature in curl‘s output anywhere.

    So, for the top-5 happiest countries in 2022 all you need should be this:

    curl -s https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw | awk '/=== 2022 report ===/,/=== 2020 report ===/'| awk -F'[|}]' '$2~/[0-9]+/{print $5}'| head -n5
    Finland
    Denmark
    Iceland
    Switzerland
    Netherlands
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search