Jill448
Jill448

Reputation: 1793

shell script: replace the contents

I have a file with the following contents

FileName:VersionInfo.properties

Installed Version:13.7.0-2
Previous Version:13.6.0-12

FileName: main.sh

#!/bin/ksh
new_maj_version=$1 # sample content: 13.7.0
new_min_version=$2 # sample content: 4

Every time I run my main.sh I want it to edit my VersionInfo.properties file. The Installed Version should shift to Previous version and my new_maj_version and new_min_version to be placed in Installed version. How can I achieve it?

Sample output after running my main.sh

FileName:VersionInfo.properties

Installed Version:13.7.0-4
Previous Version:13.7.0-2

Upvotes: 0

Views: 65

Answers (2)

iruvar
iruvar

Reputation: 23374

This should do the trick

 sed -e "/Installed Version/{s/Installed Version:\(.*\)/Installed Version:$major_version-$minor_version\nPrevious Version:\1/; n}" -e '/Previous Version/{d}'
VersionInfo.properties

Upvotes: 0

Kent
Kent

Reputation: 195179

try this: (save it to your main.sh):

#!/bin/ksh
awk -F: -v a="$1" -v i="$2" 'NR==1{n=$2;print $1":"a"-"i;next}{print $1":"n}'  /VersionInfo.properties > /tmp/tmpVersion && mv /tmp/tmpVersion VersionInfo.properties

try with

main.sh "13.7.0" "4"

Upvotes: 1

Related Questions