需要从bash中的字符串获取子字符串

时间:2019-02-08 21:11:31

标签: regex bash grep

我正试图在bash中获得Atom版本。这正则表达式工作,但我需要从字符串的子字符串,这给grep。如何从该字符串获取版本?

<span class="version">1.34.0</span>

curl https://atom.io/ | grep 'class="version"' | grep '[0-9]\+.[0-9]\+.[0-9]\+'

2 个答案:

答案 0 :(得分:2)

awk

$ curl ... | awk -F'[<>]' '/class="version"/{print $3; exit}'

答案 1 :(得分:0)

您可以通过使用cut命令并添加各自的定界符来实现。在您的情况下,这将是封装版本的><标签。

输入

curl -s https://atom.io/ \
| grep 'class="version"' \
| grep '[0-9]\+.[0-9]\+.[0-9]\+' \
| cut -d '>' -f2 \
| cut -d '<' -f1

输出

1.34.0

*添加了curl -s标志,以使输出保持沉默,个人选择