Extract version using Regular expressions in bash

bash, regex

Solution

Use pure BASH:

s='my_archive_1.1.1.201_x86_64.tgz'
[[ $s =~ ^[^_]+_[^_]+_(([^.]+\.){2}[^.]+)\.([^_]+) ]] && \
        echo "${BASH_REMATCH[1]}, ${BASH_REMATCH[3]}"

OUTPUT:

1.1.1, 201

Using your own regex:

[[ $s =~ ([A-Za-z_]+)_([0-9]+\.[0-9]+\.[0-9]+).([0-9]+)_x86_64\.tgz ]] && \
        echo "${BASH_REMATCH[2]}, ${BASH_REMATCH[3]}"

Problem

All I need to do is extract the versioning information from the following file: ``` my_archive_1.1.1.201_x86_64.tgz ``` I am trying to extract both the version number which is `1.1.1` and the release number which is `201`. Normally I use python for these purposes, but I have been asked not to. How do I do it by just using bash? The filename will always be of the form ``` ([A-Za-z_]+)_([0-9]+\.[0-9]+\.[0-9]+)\.([0-9]+)_x86_64\.tgz ``` The groups are in parenthesis. I need the second and third groups if you start counting from 1.

Original source