如何使用另一个命令的部分结果执行命令?

How to execute command with partial result from another command?

我有:

$ module avail firefox --latest
---------------------------------------------------------- /env/common/modules -----------------------------------------------------------
firefox/83.0

我想将上述结果的 firefox/83.0 部分提取到一个新命令中:

$ module load firefox/83.0

这是我目前所拥有的,但我似乎无法将 grep 的结果传输到下一个命令:

$ module avail firefox --latest | grep firefox | echo module load 

注意:这是 Modules 的版本,没有 module load app/latest 能力。

module load "$(module avail firefox --latest | grep -Eo 'firefox\/[[:digit:]]+\.[[:digit:]]+')"

使用 module avail 命令的输出,然后搜索 firefix,后跟一个 / 然后一个数字一次或多次,一个句点然后一个数字一次或多次。使用此输出作为模块加载命令的一部分

使用正则表达式仅捕获 firefox/83.0,然后使用 command substitution 在下一个命令中使用该结果;

module load $(module avail firefox --latest | sed -nre 's/^firefox\/[^0-9]*(([0-9]+\.)*[0-9]+).*/[=10=]/p'

有关正则表达式的更多信息:How to extract a version number using sed?


使用xargs

module avail firefox --latest | sed -nre 's/^firefox\/[^0-9]*(([0-9]+\.)*[0-9]+).*/[=11=]/p' | xargs module load

it does not seem like I can pipe the result from the grep onto the next command:

可以,但您需要使用其他命令,该命令将从管道中读取并将其添加为命令参数。就像:

$ module avail firefox --latest | grep firefox | xargs module load