-1

I want to access a text file and read some of its contents to change them with a new value.

This Find and Replace is really helpful but my requirement is slightly different.

Say if these are my file contents

image.description=Template Image 08182015
image.version=1.3.111
baseline.name=001_08_18_2015

I want to change them to

image.description=Template Image 08192015   #19 instead of 18
image.version=1.3.112                       #112 instead of 111
baseline.name=001_08_19_2015                #19 instead of 18

At any point of time I will not be knowing what is the value of each variable but all I know is the variable name like "Image version" So now I need some script to find what is the value of variable image.version and auto increment the value to the next possible integer.

Any suggestions/ thoughts?

Community
  • 1
  • 1
Thulasi
  • 126
  • 3
  • 19
  • 1
    since you found some related answer, what did you try? show your attempts, you may be close to the solution! – fedorqui Aug 19 '15 at 15:21
  • Is the value of the image.description always `Template Image MMDDYYYY` ? Or could there be different (or no) words before the date? Could there be words after the date? – jas Aug 19 '15 at 16:58
  • @jas, It will always be the same. only the numeric content will change based on current date – Thulasi Aug 19 '15 at 17:16
  • 1
    You say auto-increment but if the value on the first line is `08312015` instead of `08192015`, what should it be incremented to? If the answer's not `08322015` then it becomes quite a different problem from just auto-incrementing a number. – Ed Morton Aug 19 '15 at 17:32
  • @EdMorton yes you are correct. There are 2 cases here 1) To increment the integer to the next number. 2) If it is a date then it should be the current date and month and year. – Thulasi Aug 19 '15 at 19:03

1 Answers1

1

With GNU awk for the 3rd arg to match():

$ awk 'match($0,/(image\.version.*\.)(.*)/,a){$0=a[1] a[2]+1} 1' file
image.description=Template Image 08182015
image.version=1.3.112
baseline.name=001_08_18_2015

For the others since no arithmetic is involved you should be able to just use gensub() similarly.

Or, maybe this is the kind of thing you're looking for:

$ cat tst.awk
BEGIN { FS=OFS="=" }
$1=="image.description" { match($2,/.* ../); $2=substr($2,1,RLENGTH) substr($2,1+RLENGTH,2)+1 substr($2,3+RLENGTH) }
$1=="image.version"     { match($2,/.*\./);  $2=substr($2,1,RLENGTH) substr($2,1+RLENGTH)+1 }
$1=="baseline.name"     { split($2,a,/_/);   $2=a[1]"_"a[2]"_"a[3]+1"_"a[4] }
{ print }

$ awk -f tst.awk file
image.description=Template Image 08192015
image.version=1.3.112
baseline.name=001_08_19_2015
Ed Morton
  • 188,023
  • 17
  • 78
  • 185