2

I have one file like below,let say file name is file1.txt:

s.version      = "11.7.10"

Here I have to increment the last number by 1, so it should be 1+1=2 like..

s.version      = "11.7.11"

Is there any way for this. Thanks in advance.

ldward
  • 63
  • 8

1 Answers1

3

I would go with Perl, as follows:

perl -pe '/s.version/ && s/(\d+)(")/$1+1 . $2/e' file.txt

That says... "On any line where you find "s.version", substitute the last one or more digits followed by a double quote, with whatever those digits were plus one and the double quote"

So, if your file contains this:

fred
s.version      = "11.7.10"
s.version      = "11.7"
s.version="12.99.99"
frog

You will get this:

fred
s.version      = "11.7.11"
s.version      = "11.8"
s.version="12.99.100"
frog

If you want Perl to edit the file in-place (i.e. overwrite the input file), you can use the -i option:

perl -i.orig -pe '/s.version/ && s/(\d+)(")/$1+1 . $2/e' file.txt

and then your input file will be overwritten, but a backup saved in file.txt.orig

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • 2
    p cancels out n flag.Also can be more consise `perl -pe 's/s.version.*\K\d+/$&+1/e'` – 123 Jul 14 '16 at 09:07
  • 1
    @123 Thank you for your insights. I have removed the `-n` flag as you suggested. The `K` syntax is beyond my meagre `Perl` skills and I cannot explain it satisfactorily, so I would prefer not to try and *sell* it as my own! If you want to put it as an answer, I'll be more than happy to up-vote you :-) – Mark Setchell Jul 14 '16 at 10:04
  • From perlre `\K [6] Keep the stuff left of the \K, don't include it in $&`. Feel free to use it in your answer, or not, up to you. I won't be writing an answer though :) – 123 Jul 14 '16 at 10:15