2

I'm trying to get Atom version in bash. Thid regex is working, but I need a substring from string, which giving grep. How can I get version from this string?

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

curl https://atom.io/ | grep 'class="version"' | grep '[0-9]\+.[0-9]\+.[0-9]\+'
aLUc8mgO
  • 23
  • 2
  • Possible duplicate of [Extract substring in Bash](https://stackoverflow.com/questions/428109/extract-substring-in-bash) – Arc676 Feb 08 '19 at 21:14
  • Note if you have Gnu grep you can use the -P option for perl-style regex's and the -o option to print only what matches the pattern. Try this as your last command in the pipeline:`grep -oP "\d+\.\d+\.\d+"` Oh and notice escaping the dot for a literal dot, otherwise it means any character. – Gary_W Feb 08 '19 at 22:14

2 Answers2

2

with awk

$ curl ... | awk -F'[<>]' '/class="version"/{print $3; exit}'
karakfa
  • 66,216
  • 7
  • 41
  • 56
0

You can achieve this by using the cut command and adding your respective delimiters; in your case this would be the > and < tags encapsulating the version.

Input:

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

Output:

1.34.0

*added the curl -s flag to make output silent, personal choice

jonroethke
  • 1,152
  • 2
  • 8
  • 16