0

I have a text file with contents:(version.txt)

#Project Name: XYZ
#Module Name: ABC
#
#
currentVersion=9.2.04

Now I have a shell script as shown below which replaces last line of version.txt to currentVersion=9.2.05

script:

ls_version=`grep -i currentVersion version.txt | awk '{print $NF}'`
counter=`echo $ls_version | awk -F . '{print $NF}'`
fs_version=`echo $ls_version | awk -F"." -v OFS='.' '{$NF=""; print $0}'`
echo "counter b4 addition: $counter"
counter=`expr ${counter} + 1`
version=`echo ${fs_version}$counter`
echo "ls_version: $ls_version"
echo "fs_version: $fs_version"
echo "counter post addition: $counter"
echo "final version: $version"
sed -i -e "s/\(currentVersion=\).*/\1$version/" version.txt

but instead it gives o/p like this:

currentVersion=05
currentVersion=05
currentVersion=05
currentVersion=05
currentVersion=05
currentVersion=05
currentVersion=05

Can you please tell me what is wrong in this script?

Thanks in advance

Celestial
  • 139
  • 2
  • 12
  • 3
    There are so many things wrong.... don't pipe grep to awk, don't store the output of awk and then use echo to pipe it to another awk, don't use expr, don't use 'sed -i', use $() instead of backticks. This is not an exhaustive list. – William Pursell Oct 23 '17 at 16:52
  • 1
    I cannot see anything in your final `sed` that would cause the contents of the file to have each line replaced with "currentVersion=05". To debug this, stop using `sed -i` and show the actual output (eg, what happened to "counter b4 addition"? Perhaps the code you are running is not the code you are showing), the actual input, and the actual code. – William Pursell Oct 23 '17 at 17:02
  • @William Pursell, thanks for your response. After removing sed -I . I see no change/update in version.txt file. – Celestial Oct 23 '17 at 17:56
  • code execution: `counter b4 addition: currentVersion=04 ls_version: currentVersion=04 fs_version: empty counter post addition:05 final version : current_Version=05 (multiple entries)` – Celestial Oct 23 '17 at 18:00

2 Answers2

1

Following awk will help you to increase the version in your Input_file.

awk -F'.' '/currentVersion/{$3=sprintf("%02d",$3+1)"}"} 1' OFS="."   Input_file

Output will be as follows.

{#Project Name: XYZ
#Module Name: ABC
#
#
currentVersion=9.2.05}
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

Based on this, I wrote :

 perl -pe 's/^currentVersion=((\d+\.)*)(\d+)(.*)$/$1.(sprintf("%02d", $3+1)).$4/e' file.txt
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223