为什么将我的代码转换为可执行文件并运行时,我没有获得任何输出?

huangapple go评论65阅读模式
英文:

Why am I not getting any output when I convert my code into an executable and run it?

问题

I noticed you want a translation of the provided code portion without any other content. Here's the translated code:

我正在尝试编写一段代码,以列出2022年世界幸福报告中根据幸福指数排名的国家名称,该报告以HTML格式提供,链接如下:https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw 我的响应应为:

    芬兰
    挪威
    丹麦
    冰岛
    瑞士
    荷兰

从我附加的链接中可以看出,我注意到所有国家都有一个共同之处,那就是它们都在2个方括号[[国家名称]]内,因此我决定使用`awk`来帮助我,如下所示:

    echo "$input" | awk 'BEGIN{RS="[[ *]]"} NR>1'
    
但是,似乎存在问题,因为我的代码无法运行,您有关于我应该修复什么或如何解决这个问题的建议吗?
英文:

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

As you can see from the link I've attached, I noticed a pattern that all the countries have one thing in common, which is that they are within 2 square brackets [[ country names ]], so I decided to use awk to help me out like this:

echo "$input" | awk 'BEGIN{RS="[[ *]]"} NR>1' 

However, there seems to be an issue since my code wouldn't run, any suggestions on what should I fix or how to approach this problem?

答案1

得分: 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)

英文:

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)"

答案2

得分: 1

你组织代码的方式略有问题。我已经稍微调整了你的代码,并建议你使用grep来过滤掉方括号内包含任何小写或大写字母的数据,因为这是提到的国家的一个显著特征。类似grep -E '^[A-Za-z ]+$'的内容对你有用。

我会留给你来决定在何处以及如何实现这段代码。祝你好运!

英文:

The way you organized your code is slightly off. I've played around with your code a bit and I'd recommend using grep to filter out data which contains any lowercase or uppercase alphabets within the enclosed square brackets, since it's a distinct characteristic of the countries mentioned. Something like grep -E '^[A-Za-z ]+$' should be useful for your cause.

I'll leave it to you to figure out where and how to implement the code. Good Luck!

答案3

得分: 0

这并不能解决所有问题,但至少它仅限于国家列表:(不幸的是,我无法让它在没有额外gcat -的情况下工作)

英文:

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 ===

答案4

得分: 0

我不确定我完全理解你试图实现的目标,特别是与sort有关的部分,我理解的方式是列表已经按从“最幸福”到“最不幸福”的顺序排好了。

此外,你似乎不太了解你的数据实际是什么样的 - 你试图定义一个awk范围,但字符串=== 2021 report ===curl的输出中并没有出现。

所以,对于2022年最幸福的前5个国家,你只需要这样做:

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
英文:

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

huangapple
  • 本文由 发表于 2023年5月11日 20:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定