1

I have a file that contains this string requires="ua-common/2.7.25@aep/stable" (the version number is variable) and I need to replace it with requires="ua-common/2.7.26@aep/stable" (the version number is variable). So I need to change the version number without knowing what its value is. I need to make a little script like so:

#!/bin/bash

echo "Insert version number: "
read version
#replace $version with old version in file

Thank you

4
  • 1
    So requires="ua-common/${version}@aep/stable" is what in your script? Commented Jul 14, 2021 at 8:46
  • 1
    Please confirm if you always have string requires="ua-common/2.7.25@aep/stable" in same format? OR it could be in some different format also? Commented Jul 14, 2021 at 8:58
  • The format is always requires="ua-common/unknown@aep-stable" Commented Jul 14, 2021 at 9:03
  • @Fausto01 Without more clear specification, this is your answer: newRequires="ua-common/$version@aep/stable" Commented Jul 14, 2021 at 11:52

4 Answers 4

1

This is how I understood your question.

The file file.txt

fjlakjflajkflkajfjakjfalkjfoairujnasncv
O
aljflajflja ljfaljflakjflakjf
ia;jflajfjaljfajflajfoiuqoruaf
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
requires="ua-common/2.7.25@aep-stable"
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
jalfjalkfjaoeiurjasnvafnaojf
jvjvg]
iajfiaufurva ajfaj

The script myscript

#!/usr/bin/env bash

current_version=$(grep -oP 'requires="ua-common\/\K.*?(?=@)' file.txt)

printf 'The current version is: %s\n\n' "$current_version"

read -rp "Insert a new version number: " version

if [[ -n $version ]]; then
  sed "s|\(requires=.*ua-common/\).*\(@.*\)\$|\1$version\2|" file.txt
fi

Then run

bash ./myscript

Output

The current version is: 2.7.25

Insert a new version number: 

Key in the new version number:

The current version is: 2.7.25

Insert a new version number: 2.7.26

Output

fjlakjflajkflkajfjakjfalkjfoairujnasncv
O
aljflajflja ljfaljflakjflakjf
ia;jflajfjaljfajflajfoiuqoruaf
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
requires="ua-common/2.7.26@aep-stable"
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
jalfjalkfjaoeiurjasnvafnaojf
jvjvg]
iajfiaufurva ajfaj

There are some more to do with the current script like, error checking , exit when something went wrong and so on, but that is a start.

Sign up to request clarification or add additional context in comments.

Comments

1

1st solution: With your shown samples(this will work with any version shown in samples format), please try following awk code. This will give output as ua-common/2.7.26@aep/stable with shown samples.

echo "$requires" | 
awk '
  match($0,/([0-9]+\.)*[0-9]+/){
    value=substr($0,RSTART,RLENGTH)
    num=split(value,arr,".")
    arr[num]+=1
    value=""
    for(i=1;i<=num;i++){
      value=(value?value ".":"")arr[i]
    }
    print substr($0,1,RSTART-1) value substr($0,RSTART+RLENGTH)
}
'


2nd solution: If your input is always same and you want to increase digit just before @ then try following.

echo "$requires" | 
awk '
  match($0,/[0-9]+@/){
    print substr($0,1,RSTART-1) substr($0,RSTART+1,RLENGTH-1)+1 substr($0,RSTART+RLENGTH-1)
}
'

Comments

1

The most obvious solution since you ask for bash (doesn't work with dash which is mostly linked to /bin/sh)

#!/bin/bash
oldVersion=2.7.25
newVersion=2.7.26
requires="ua-common/2.7.25@aep/stable"
echo "${requires//$oldVersion/$newVersion}"

and the output:

ua-common/2.7.26@aep/stable

Reference page: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

2 Comments

And if I don't know the old version?
@Fausto01 then your question was not clearly asked. even newRequires="ua-common/$newVersion@aep/stable" seems a valid answer. or do you want to increment the existing version?
0

You might want to use sed for that job. Depending of the file contents you need to adjust the sed searchpattern:

#: cat test.file
requires="ua-common/2.7.25@aep/stable"
#: sed -i 's/^requires=\"ua-common\/[0-9]*.[0-9]*.[0-9]*.@/requires=\"ua-common\/1.2.3@/g' test.file
#: cat test.file
requires="ua-common/1.2.3@aep/stable"

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.