0

I'm trying to write a sed command to increment the below release number in a file.txt. Please help me out.

versionAndDate: 'Release: 1.3

I want the output to be incremental to below value:

versionAndDate: 'Release: 1.4.1
versionAndDate: 'Release: 1.4.2
versionAndDate: 'Release: 1.4.3
versionAndDate: 'Release: 1.4.4
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
MSC
  • 1
  • Thanks for the response Jared. So let me give you more clarity on this. We are using AWS CodeCommit as our repo and in the code appconfig.js we have versionAndDate: 'Release: 1.3. In the buildspec.yml, I need to write a SED or Perl or awk command so that for every build the versionAndDate: should be updated to an incremental number like below. For the first build it should be updated to versionAndDate: 'Release: 1.4.1 and for the second build it should be updated to versionAndDate: 'Release: 1.4.2 and it should be in incremental order – MSC Mar 16 '22 at 17:01
  • Do any of these answers solve your problem? [How to increment version number in a shell script?](https://stackoverflow.com/questions/8653126/how-to-increment-version-number-in-a-shell-script) – jared_mamrot Mar 16 '22 at 23:34
  • I tried with SED but it didn't work. Can you provide me the exact command? Input = versionAndDate: 'Release: 1.3. I need the output to be as versionAndDate: 'Release: 1.4.0. For the next build it should be 1.4.1 and incremental. – MSC Mar 17 '22 at 17:27
  • To get from 1.3 to 1.4.0 you can use the command `sed "s/versionAndDate: \'Release: 1.3/versionAndDate: \'Release: 1.4.0/g" file.txt`, then to incrementally increase the version number use one of the methods in the link I posted above (https://stackoverflow.com/questions/8653126/how-to-increment-version-number-in-a-shell-script) – jared_mamrot Mar 17 '22 at 22:38
  • Typically software release versions are updated 'manually' as you rarely have a clear schedule for major version updates e.g. 1.3 to 1.4 is normally considered a major update, 1.4 to 1.4.1 is normally considered a minor update, but you don't necessarily go from v1.4.9 to v1.5.0 unless you are making a major update to the software (you go to 1.4.10; see https://en.wikipedia.org/wiki/Software_versioning for more details). So unless there is a clear logic to follow it's difficult to provide a script to automatically change the version. Does that make sense? – jared_mamrot Mar 17 '22 at 22:51
  • @jared_mamrot : Thanks for sending the sed command. I already figured out the sed command to replace the versionAndDate: 'Release: 1.3 to versionAndDate: 'Release: 1.4. But I'm looking for sed command or awk command with the incremental logic for every build. Can you help me with that command? – MSC Mar 22 '22 at 22:24
  • I have updated my original answer; if that doesn't fix your problem, try https://artem.services/?p=1140&lang=en and/or post another question on SO – jared_mamrot Mar 23 '22 at 02:25
  • Hey Jared. The updated answer doesn't work with my scenario. I need to write the sed command in buildspec.yml file. It should be like sed -i "21 s%/versionAndDate: 'Release: 1.3'/%/versionAndDate: 'Release: 1.4.0'/%" ./config/appconfig.js. Can you give the output in the above format with incremental number? – MSC Mar 29 '22 at 22:42
  • I suggest you post a new question with all the information you've included here i.e. "We are using AWS CodeCommit and "appconfig.js" has the current version listed ("versionAndDate: 'Release: 1.3"). I want to edit the "buildspec.yml" file so that for every build the 'version' is incremented (i.e. from 1.3 to 1.4, or from 1.4.0 to 1.4.1)". Is this possible using `sed`/`awk`/`perl`? – jared_mamrot Mar 29 '22 at 22:51

1 Answers1

-1

Using sed

$ cat input_file
versionAndDate: 'Release: 1.3
$ cat chg_ver.sh
#!/usr/bin/env bash

max_version=4
for ((i=1; i<=max_version; i++)); do 
    sed "s/\(.*: \).*/\11.4.$i/" input_file >> output_file
done
$ cat output_file
versionAndDate: 'Release: 1.4.1
versionAndDate: 'Release: 1.4.2
versionAndDate: 'Release: 1.4.3
versionAndDate: 'Release: 1.4.4

Using awk

$ awk -v max_version=4 '{sub(/1.3/,"1.4.");for (i=1; i<=max_version; i++) print $0 i}' input_file
versionAndDate: 'Release: 1.4.1
versionAndDate: 'Release: 1.4.2
versionAndDate: 'Release: 1.4.3
versionAndDate: 'Release: 1.4.4
HatLess
  • 10,622
  • 5
  • 14
  • 32
  • Thanks for the response. So let me give you more clarity on this. We are using AWS CodeCommit as our repo and in the code appconfig.js we have versionAndDate: 'Release: 1.3. In the buildspec.yml, I need to write a SED or Perl or awk command so that for every build the versionAndDate: should be updated to an incremental number like below. For the first build it should be updated to versionAndDate: 'Release: 1.4.1 and for the second build it should be updated to versionAndDate: 'Release: 1.4.2 and it should be in incremental order. – MSC Mar 16 '22 at 15:07