如何从网站的特定行进行卷曲?

如何从网站的特定行进行卷曲?

我正在尝试卷曲 https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw到我的文件我只想要 ===2018 报告 === 部分

这是我写的

curl curl https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw >> a1t4.txt

但它获取了该网站的所有内容我想要的只是这部分

===2018 report===
The 2018 report features the happiness score averaged over the years 2015–2017. As per the 2018 Happiness Index, [[Finland]] is the 'happiest' country in the world. [[Norway]], [[Denmark]], [[Iceland]] and [[Switzerland]] hold the next top positions. The report was published on 14 March 2018 by UN. The full report can be read at [http://worldhappiness.report/ed/2018/ 2018 Report]. The World Happiness Report is a landmark survey of the state of global happiness. The World Happiness Report 2018, which ranks 156 countries by their happiness levels, and 117 countries by the happiness of their immigrants, was released on March 14 at a launch event at the Pontifical Academy of Sciences in the Vatican.
{{collapse top|title=Table}}
{| class="wikitable sortable"
|- valign=top
! style="width: 10px;" | Overall rank
! style="width: 250px;" | Country or region
! {{abbr|Score|Happiness score}}
! style="width: 10px;" | {{abbr|GDP per capita|Explained by: GDP }}
! style="width: 10px;" | {{abbr|Social support|Explained by: Social support}}
! style="width: 10px;" | {{abbr|Freedom to make life choices|Explained by: Freedom to make life choices}}
! style="width: 10px;" | {{abbr|Generosity|Explained by: Generosity}}
! style="width: 10px;" | {{abbr|Perceptions of corruption|Explained by: Perceptions of corruption}}
|-
| 1||{{flag|Finland}}||7.632||1.305||1.592||0.874||0.681||0.202||0.393
|-
| 2||{{flag|Norway}}||7.594||1.456||1.582||0.861||0.686||0.286||0.340
|-
| 3||{{flag|Denmark}}||7.555||1.351||1.590||0.868||0.683||0.284||0.408
|-
| 4||{{flag|Iceland}}||7.495||1.343||1.644||0.914||0.677||0.353||0.138
|-
| 5||{{flag|Switzerland}}||7.487||1.420||1.549||0.927||0.660||0.256||0.357
|-
| 6||{{flag|Netherlands}}||7.441||1.361||1.488||0.878||0.638||0.333||0.295
|-
| 7||{{flag|Canada}}||7.328||1.330||1.532||0.896||0.653||0.321||0.291
|-
| 8||{{flag|New Zealand}}||7.324||1.268||1.601||0.876||0.669||0.365||0.389
|-
| 9||{{flag|Sweden}}||7.314||1.355||1.501||0.913||0.659||0.285||0.383
|-
| 10||{{flag|Australia}}||7.272||1.340||1.573||0.910||0.647||0.361||0.302

答案1

尝试awk

curl -s 'https://en.wikipedia.org/wiki/World_Happiness_Report?action=raw' \
| awk  '
    /^===2018 report===/{p=1}
    /^\| 11\|\|/{p=0}
    p'
  • 我们通过管道将输出从curltoawk使用curl ... | awk ...
  • awk当行首匹配时,脚本将变量设置p为(true) ,当行首匹配时,脚本将变量设置为 (false) 。只要是 1 就会打印该行。1===2018 report===0|11 ||p

相关内容