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
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: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 -
)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 incurl
‘s output anywhere.So, for the top-5 happiest countries in 2022 all you need should be this: