将 T 管分为 3 个不同的进程并进行第二场比赛

Tee pipe into 3 different processes and grepping the second match

我正在尝试创建一个 bash 脚本,它向我显示有关德国和瑞士以及全世界的电晕感染人数的最新统计数据。

corona () {
    curl -s https://corona-stats.online\?minimal\=true | tee >(head -n 1) > >(grep "(CH)\|(DE)")
    curl -s https://corona-stats.online\?minimal\=true | tail -n 20 | grep World
}

如您所见,为此我必须创建一个非常难看的脚本,其中 curl 被调用了两次。我必须这样做,因为网站看起来像这样:

Rank World                               Total Cases  New Cases ▲ Total Deaths New Deaths ▲ Recovered  Active    Critical Cases / 1M pop
1    USA (US)                               7,497,256     2,585 ▲      212,694         34 ▲  4,737,369 2,547,193   14,190         22,617
2    India (IN)                             6,397,896     5,936 ▲       99,833         29 ▲  5,352,078   945,985    8,944          4,625
3    Brazil (BR)                            4,849,229                  144,767               4,212,772   491,690    8,318         22,773
4    Russia (RU)                            1,194,643     9,412 ▲       21,077        186 ▲    970,296   203,270    2,300          8,185
...
22   Germany (DE)                             295,943       413 ▲        9,586                 259,500    26,857      362          3,529
...
58   Switzerland (CH)                          54,384       552 ▲        2,075          1 ▲     45,300     7,009       32          6,272
...
     World                                 34,534,040    63,822 ▲    1,028,540      1,395 ▲ 25,482,492 8,023,008   66,092       4,430.85


Code: https://github.com/sagarkarira/coronavirus-tracker-cli
Twitter: https://twitter.com/ekrysis

Last Updated on: 02-Oct-2020 12:10 UTC

US STATES API: https://corona-stats.online/states/us
HELP: https://corona-stats.online/help
SPONSORED BY: ZEIT NOW
Checkout fun new side project I am working on: https://messagink.com/story/5eefb79b77193090dd29d3ce/global-response-to-coronavirus

我只想显示table(世界)的第一行,最后一行和关于德国和瑞士的两行。我设法通过将 curl 的输出输送到 head -n 1 并搜索国家代码来显示第一行和两个国家。由于 this 的回答,我能够做到这两件事。

现在我想获取 table 中的最后一行,其中显示了整个世界的当前病例。我尝试再次使用 tee 将其通过管道传输到第三个进程 tee >(head -n 1) > >(grep "(CH)\|(DE)") > >(tail -n 20 | grep World)。但这没有用。我的第一个问题是,如何使用 tee 将输出通过管道传输到 3 个不同的进程?

第二个问题围绕着我尝试grep World 行的方式。我跟踪最后 20 行,然后 grep“世界”。我这样做是因为如果我简单地 grep“世界”,它只会 return 也可以找到“世界”的标题行。所以我的第二个问题是:如何仅 grep 最后一次或第二次出现?

您可以链接多个 tee 命令并仅丢弃 tee 的最后一个输出:

curl -s ... | tee >( cmd1 ) | tee >( cmd2 ) | tee > >( cmd3 )

其实我们可以缩短为:

curl -s ... | tee >( cmd1 ) | tee >( cmd2 ) | cmd3

因为我们不使用最后一个 tee 的输出。

同时向终端写入多个命令可能会混淆输出。一个更优雅的解决方案是只使用一个 grep,例如

curl -s ... | grep '(DE)\|(CH)\|World.*,'

表达式World.*,只会在World之后的同一行中寻找逗号,以排除标题行。

你可以试试这个:

curl -s https://corona-stats.online\?minimal\=true | grep -E "(Rank|^1[^0-9]|\(CH\)|\(DE\))"

使用 grep 仅显示包含“Rank”、1[non-digit]、(CH)、(DE)

的行

我认为变量应该更适合您的需要(至少在这种情况下),例如:

corona() {
  data="$(curl -s https://corona-stats.online\?minimal\=true)"
  echo "$data" | head -n 1
  echo "$data" | grep "(CH)\|(DE)"
  echo "$data" | tail -n 20 | grep World
}

它会更容易传达您正在尝试做的事情,并且如果您需要更改任何内容,也会更容易扩展。