Please be aware that parsing HTML with non-html tools it fraught with peril; you will see that this works, and assume you can get away with it always. You'll spend hours trying to get the next level of complexity to work, when you should be studying how to use html-aware tools. Don't say we didn't warn you (-;, but
printf "<http://mygithub.com/api/v3/organizations/20/repos?page=1>; rel=prev, <http://mygithub.com/api/v3/organizations/20/repos?page=3>; rel=next, <http://mygithub.com/api/v3/organizations/20/repos?page=4>; rel=last, <http://mygithub.com/api/v3/organizations/20/repos?page=1>;\n" \
| awk -F" " '{
for(i=1;i<=NF;i++){
if ($i == "rel=next,") {
gsub(/[<>]/,"",$(i-1);sub(/;$/,"",$(i-1))
print $(i-1)
}
}
}'
produces required output:
http://mygithub.com/api/v3/organizations/20/repos?page=3
To save the output of a script section into a variable, you wrap the code for command-substitution, in this case
nextReposLink=$( printf .... | awk '....' )
#-------------^^--------------------------^
The ^ pointed items are modern syntax for command-substitution. The code inside of $( ... ) is executed and the standard output is passed as a argument to the invoking command line. (The original syntax for command substitution is/was `cmds` and works the same in the simple case var=`cmds` . You can nest modern cmd-substitution easily, whereas the old version requires a lot of escape character fiddling. Avoid it if you can.
Note that about any s/str/rep/ that sed can do, awk can do the same, but requires the use of the sub(/regx/, "repl", "str") or gsub(sameArgs) functions. In this particular case, you may need to escape the <> like \<\>.
Be sure to always dbl-quote the use of variables, i.e. echo "$nextReposLink".
IHTH
/bin/sh, or is this running in bash, ksh, zsh, or another extended shell? If you're in a shell with native regex support, you should consider using that.BASH_REMATCHin extract substring using regexp in plain bash. Usingsedis generally best avoided when you're running it with only one line of input per invocation -- it takes a lot of time to start up each copy, even though it's quite fast once it's running.echo $nextReposLink. - prints the string with mygithub links I want to save the result of the command in a new variable...$nextReposLink | awk '{for (i=0; i<=NF; i++){if ($i == "rel=next,"){print $(i-1);exit}}}' | sed -e 's/</ /' -e 's/>;/ /'Something like, but that gives me a "bad substitution"x="${echo $nextReposLink | awk '{for (i=0; i<=NF; i++){if ($i == \"rel=next,\"){print $(i-1);exit}}}'}"