The first task is to extract the rate from that line. With GNU grep (non-embedded Linux or Cygwin), you can use the -o
option. The part you want is the one containing only digits, and followed by a %
sign. If you don't want to extract the %
itself, you need an additional trick: a zero-width lookahead assertion, which matches nothing but only if that nothing is followed by %
.
command1 -p=aaa -v=bbb -i=4 | grep -o -P '[0-9]+(?=%)'
Another possibility is to use sed. To extract a part of a line in sed, use the s
command, with a regex that matches the whole line (starting with ^
and ending with $
), with the part to retain in a group (\(…\)
). Replace the whole line by the content of the group(s) to keep. In general, pass the -n
option to turn off default printing and put the p
modifier to print lines where there is something to extract (here there's a single line so it doesn't matter). See Return only the portion of a line after a matching pattern and Extracting a regex matched with 'sed' without printing the surrounding characters for more sed tricks.
command1 -p=aaa -v=bbb -i=4 | sed 's/^.*rate(\([0-9]*\)%).*$/\1/'
More flexible again than sed, is awk. Awk executes instructions for each line in a small imperative language. There are many ways to extract the rate here; I select the second fields (fields are delimited by whitespace by default), and remove all the characters in it that are not a digit.
command1 -p=aaa -v=bbb -i=4 | awk '{gsub(/[^0-9]+/, "", $2); print $2}'
The next step, now that you've extracted the rate, is to pass it as an argument to command2
. The tool for that is a command susbtitution. If you put a command inside $(…)
(dollar-parenthesis), its output is substituted into the command line. The output of the command is split into separate words at each whitespace block, and each word is treated as a wildcard pattern; unless you want this to happen, put double quotes around the command substitution: "$(…)"
. With the double quotes, the output of the command is used directly as a single parameter (the only transformation is that newlines at the end of the output are removed).
command2 -t "$(command1 -p=aaa -v=bbb -i=4 |
sed 's/^.*rate(\([0-9]*\)%).*$/\1/')"