简体   繁体   English

Sed 字符串中间有两个条件

[英]Sed the middle of string with two conditions

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我怎样才能在 1 个命令中使用 2 个条件的 sed,还有什么比这个解决方案更好,我想取最新版本 geckodriver 的名称,所以我这样做

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

the output of this is output 是

<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我只想拿这个 output

v0.26.0

so i am using sed所以我正在使用 sed

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

and the output is output 是

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

How can i take only v0.26.0我怎么能只用v0.26.0

i can't use awk.我不能使用 awk。 thank you for your advince.谢谢你的建议。

You may use您可以使用

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

Here,这里,

  • -n suppresses default line output -n禁止默认行 output
  • .*/tag/\([^"]*\).* matches .*/tag/\([^"]*\).*匹配
    • .* - any 0+ chars .* - 任何 0+ 个字符
    • /tag/ - /tag/ text /tag/ - /tag/文本
    • \([^"]*\) - captures into Group 1 any 0 or more chars other than " \([^"]*\) - 将除"之外的任何 0 个或多个字符捕获到第 1 组中
    • .* - any 0+ chars .* - 任何 0+ 个字符
  • \1 keeps the value of Group 1 in the result \1将第 1 组的值保留在结果中
  • p prints the result. p打印结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM