Sed the middle of string with two conditions

Viewed 40

How can i take sed with 2 condition in just 1 single command, and, is there anything else better than this solution, i want to take then name of latest version geckodriver, so i doing this

curl -s https://github.com/mozilla/geckodriver/releases/latest

the output of this is

<html><body>You are being <a href="https://github.com/mozilla/geckodriver/releases/tag/v0.26.0">redirected</a>.</body></html>

i want to take only this output

v0.26.0

so i am using sed

curl -s https://github.com/mozilla/geckodriver/releases/latest | sed 's/.*tag[/]//'

and the output is

v0.26.0">redirected</a>.</body></html>

How can i take only v0.26.0

i can't use awk. thank you for your advince.

1 Answers

You may use

sed -n 's~.*/tag/\([^"]*\).*~\1~p'

Here,

  • -n suppresses default line output
  • .*/tag/\([^"]*\).* matches
    • .* - any 0+ chars
    • /tag/ - /tag/ text
    • \([^"]*\) - captures into Group 1 any 0 or more chars other than "
    • .* - any 0+ chars
  • \1 keeps the value of Group 1 in the result
  • p prints the result.

See an online demo:

#!/bin/bash
s='<html><body>You are being <a href="https://github.com/mozilla/geckodriver/releases/tag/v0.26.0">redirected</a>.</body></html>'
sed -n 's~.*/tag/\([^"]*\).*~\1~p' <<< "$s"
# => v0.26.0
Related