Fausto01
Fausto01

Reputation: 211

Bash replace string with another in determined pattern

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

Upvotes: 1

Views: 232

Answers (4)

knecht_rootrecht
knecht_rootrecht

Reputation: 11

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"

Upvotes: 0

Jetchisel
Jetchisel

Reputation: 7791

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.

Upvotes: 1

Daniel Alder
Daniel Alder

Reputation: 5372

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

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133458

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)
}
'

Upvotes: 1

Related Questions