I'm currently trying to extract a substring and version number from a filename using bash.
There are two formats the filenames will be in:
example-substring-1.1.0.tgz
example-substring-1.1.0-branch-name.tgz
For the first scenario I was able to extract the version number using sed like so:
echo example-substring-1.1.0.tgz | sed "s/.*-\(.*\)\.[a-zA-Z0-9]\{3\}$/\1/"
However this won't work for the second scenario.
Eventually I would like to create a script that will store the first substring and version in an associative array like below.
example_array["example-substring"]="1.1.0"
example_array["example-substring"]="1.1.0-branch-name"
This is proving tricky however as I can't seem to find a good way that will work for both scenarios. And for scenarios where the version includes the branch name I can't know before hand how many words the branch name will consist of.
I think variable expansion may be the way to go but wasn't able to get it to output what I want.
(.*)
, use([0-9.]*)
to match numbers. Then you don't need to worry about what's after it.sed -r
to use extended regexp without having to escape it so much.*.tgz
could you have*.tar.gz
?-<digits>
in the branch-name part, e.g.example-substring-1.1.0-branch-1.2.3.tgz
so you should include at least one of those in your sample input/output as that'd be an easy match to get wrong in a potential solution. There are probably other rainy day cases you should come up with too.