fredrik
fredrik

Reputation: 10281

How to execute command with partial result from another command?

I have:

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

I would like to extract the firefox/83.0 part of the above result into a new command:

$ module load firefox/83.0

This is what I have so far, but it does not seem like I can pipe the result from the grep onto the next command:

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

Note: this is on a version of Modules which does not have the module load app/latest ability.

Upvotes: 0

Views: 154

Answers (3)

0stone0
0stone0

Reputation: 43964

Use a regex to capture only firefox/83.0, then use command substitution to use that result in the next command;

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

More info on the regex: How to extract a version number using sed?


Using xargs;

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

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 140970

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

You can, but you need to use other command, that will read from the pipe and add it as command arguments. As simple as:

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

Upvotes: 0

Raman Sailopal
Raman Sailopal

Reputation: 12877

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

Use the output of the module avail command and then search for firefix, followed by a / and then a digit one or more times, a full stop and then a digit one or more times. use this output as part of the module load command

Upvotes: 0

Related Questions