1

I'm writing a bash script and trying to parse a file to get a software version. The output is :

Version 13.0.R7.0 - Built on Tue Feb 09 18:47:29 EST 2016 

I want to be able to pull out 13.0 (I will later cut off the .0) with the regex ^1[0-9]\.0, and pull out the R7 with R[0-9]{1}.

I know how to match them, I just don't know how to put the value that matches the regex into a variable, is such a thing possible?

1
  • Does that mean that you could use sed or something similar? Commented Jul 27, 2016 at 7:41

1 Answer 1

2

With GNU bash:

echo 'Version 13.0.R7.0 - Built on Tue Feb 09 18:47:29 EST 2016' | while IFS=" ." read -r a b c d rest; do echo $b $c $d; done 

or

while IFS=" ." read -r a b c d rest; do echo $b $c $d; done < filename 

Output:

 13 0 R7 
2
  • That works, but doesn't answer the question directly. Is there no way to pull a regex match into a variable? I've needed to do it multiple times Commented Jul 26, 2016 at 19:35
  • 1
    With an array: IFS= read -r line < filename; [[ $line =~ Version\ ([^.]*)\.([^.]*)\.([^.]*)\. ]] && echo ${BASH_REMATCH[1]} ${BASH_REMATCH[2]} ${BASH_REMATCH[3]} Commented Jul 26, 2016 at 19:43

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.