0

I am working on a bash script that will parse a full string name to provide me with a firmware value. For example the original string name will be something like:

Example_v0p1

Desired Output:

0.1

The first part of my function deliminates the string based on the underscore value and sets the second element of the array equal to a variable called version:

arr=(`echo $1| tr '_' ' '`)
version=${arr[1]}

Using the example I provided, $version now equals "v0p1". I am searching for a way to parse this string further so that it will look like the desired output.

If it is possible I would like the solution to be able to handle multiple variations of the same format so that I can use the same script for all of the version codes that could be generated. The syntax however will always be the same. v#p#{p#p#....} Examples:

v0p1p1
v3p2p0p1
v1p3

Desired output:

0.1.1 
3.2.0.1
1.3 

I am unsure of the best way to approach this problem. I am not experienced enough with REGEX's to accomplish this and I cannot come up with an appropriate way to parse the strings because they are going to be different lengths.

3 Answers 3

3

Use parameter expansion:

#!/bin/bash
for v in v0p1p1 v3p2p0p1 v1p3 ; do
    v=${v//[vp]/.}             # Replace v's and p's with dots.
    v=${v#.}                   # Remove the leading dot.
    echo "$v"
done
Sign up to request clarification or add additional context in comments.

6 Comments

I'm sorry if I confused you with the explanation. What you have declared as "v" is actually three separate strings. I was just unsure how to show that in the block
@StephenWeaver: For a single string, you can skip the second replacement. I updated the answer.
This worked nicely as well, thank you for taking the time to help.
@StephenWeaver, I'd suggest this answer as the accepted one -- if you put it inside a tight loop, you'll observe that its performance is far, far better than shelling out to sed.
@CharlesDuffy I appreciate the time it took for choroba to help me; however, I did not choose this as the best answer because I do not need the loop. anubhava provided me with exactly what I wanted. A simple solution that applies to every instance. I only require the script to handle a single variable. His answer is more acceptable for the problem he presented, it just simply was not my particular problem.
|
0

You can do something like this:

s='v3p2p0p1'
ver=$(sed 's/p/./g' <<< "${s:1}")

echo "$ver"
3.2.0.1

s='v0p1p1'
ver=$(sed 's/p/./g' <<< "${s:1}")

echo "$ver"
0.1.1

5 Comments

If you wouldn't mind can you explain the piece that says <<< "${s:1}"
<<< is called here-string to feed in some value to sed command.
Instead of echo, I need the output to be stored into a variable
Check updated answer for storing derived version in a variable.
Thank you very much for the help. That is exactly what I needed. MUCH nicer than my original approach.
0

This will work:

var="$(echo "v0p1p1" | tr -s 'vp' " .")"

It replaces v with space though.

If you don't want that you can do:

var=$(echo "v0p1p1" | tr -d 'v' | tr -s 'p' '.')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.