2

How can i parse the string below to extract version number and release number from the filename string below.

program-product_3.0.1009-5-XY1.0.456-1_i386.deb

Here I'm interested in getting

Version number : 3.0.1009
Release number : 5 

I tried grep, awk and sed and I`m not able to get it quite right.

The string follows the pattern,

Filename_Version-Release-OtherDependentPackageNameAndVersion-Release_Arch.deb. 

Please note that the naming convention is out of my control.

2
  • You should provide other examples of package names then. Commented Aug 7, 2014 at 17:23
  • konsolebox, The naming pattern does not change. I meant that the current pattern is fixed and I cant change it to fit my needs. That was just a preemptive statement in case someone suggested it. Commented Aug 15, 2014 at 19:59

3 Answers 3

3

Using pure BASH:

s='program-product_3.0.1009-5-XY1.0.456-1_i386.deb'
[[ "$s" =~ _([^-]+)-([^-]+) ]] && echo "${BASH_REMATCH[1]} : ${BASH_REMATCH[2]}"
3.0.1009 : 5
Sign up to request clarification or add additional context in comments.

4 Comments

Yes sure. First it matches an underscore then it finds ([^-]+) which will grab 3.0.1009 in input. Then it matched hyphen and captures ([^-]+) that will find 5
Thanks. But I decided to go with the awk version though.
Sure that's your choice but storing output of awk into 2 different BASH variables is another extra operation. Here you get these values in BASH variables.
No argument there. But readability trumps brevity always (at least for me). When working with large code bases worked on by hundreds of developers, in fast paced agile project like mine, we go out of our way to spell out what is happening so that code is very easy to understand and errors can be caught easily. So I always go for commonly used/popular/easily readable solutions even if it means a slight performance hit or few extra lines of code. And often, this philosophy is enforced on us during review :)
2

Here's an awk version:

awk -F'[_-]' '{print "Version number : "$3; print "Release number : "$4}'

Comments

0

With early bash version 2.05b or newer:

#!/bin/bash
F='program-product_3.0.1009-5-XY1.0.456-1_i386.deb'
IFS=- read V R __ <<< "${F#*_}"
printf 'Version number : %s\nRelease number : %s\n' "$V" "$R"

Run with:

bash script.sh

Output:

Version number : 3.0.1009
Release number : 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.