1

I have a abc.toml file having following content :

[env]
    APPLICATION_NAME = 'loy'
    COMPONENT_NAME = 'web-loy'
    AAAS_VERSION='0.0.2'
    AAAS_FLAVOR='apache-base'
    CONF_VERSION='0.0.1'
    SERVICE_VERSION='0.0.2'

I want to write a script in shell so that I can call it and increment the version no. in CONF_VERSION. i.e After calling script, CONF_VERSION should become '0.0.2', and next time '0.0.3' and so on...

Expected output after calling the script once is:

[env]
    APPLICATION_NAME = 'loy'
    COMPONENT_NAME = 'web-loy'
    AAAS_VERSION='0.0.2'
    AAAS_FLAVOR='apache-base'
    CONF_VERSION='0.0.2'
    SERVICE_VERSION='0.0.2'

2 Answers 2

1

This will increment the last digit for CONF_VERSION

awk '/CONF_VERSION/ {split($2,a,".");++a[3];$0=$1FS a[1]"."a[2]"."a[3]FS}1' FS="'" file
[env]
    APPLICATION_NAME = 'loy'
    COMPONENT_NAME = 'web-loy'
    AAAS_VERSION='0.0.2'
    AAAS_FLAVOR='apache-base'
    CONF_VERSION='0.0.2'
    SERVICE_VERSION='0.0.2'

PS you do not write what should happen after 0.0.9. This awk gives 0.0.10

Edit:

awk '/CONF_VERSION/ {split($2,a,".");if (a[3]==9) {a[3]=0;++a[2]} else {++a[3]};$0=$1FS a[1]"."a[2]"."a[3]FS}1' FS="'" file

This version will change from 0.0.9 to 0.1.0 and not 0.0.10

Sign up to request clarification or add additional context in comments.

2 Comments

The output right now is : CONF_VERSION='0.0.2 What should be edited to get output as CONF_VERSION='0.0.2'
@HarshitGoel You can do this awk 'code' abc.toml > tmp && mv tmp abc.toml
1

Here is another awk version:

$ awk -F'[.]' '/CONF_VERSION=/{$3=($3+1)"\x27"} 1' OFS=. abc.toml
[env]
    APPLICATION_NAME = 'loy'
    COMPONENT_NAME = 'web-loy'
    AAAS_VERSION='0.0.2'
    AAAS_FLAVOR='apache-base'
    CONF_VERSION='0.0.2'
    SERVICE_VERSION='0.0.2'

How it works

  • -F'[.]'

    This tells awk to use . as the field separator for input.

  • /CONF_VERSION=/{$3=($3+1)"\x27"}

    This selects only lines which contain CONF_VERSION=. For those lines, the third field is incremented by 1 and a single quote is appended.

    \x27 means a single quote. It is expressed this way so as not to confuse the shell.

  • 1

    This tells awk to print the line.

  • OFS=.

    This tells awk to use a period as the field separator for output.

To update in-place

If you have GNU awk (sometimes called gawk), use:

gawk -i inplace -F'[.]' '/CONF_VERSION=/{$3=($3+1)"\x27"} 1' OFS=. abc.toml

The following will update in place regardless of the awk version:

awk -F'[.]' '/CONF_VERSION=/{$3=($3+1)"\x27"} 1' OFS=. abc.toml >temp && mv temp abc.toml

8 Comments

To update the file abc.toml also I am using :awk -F'[.]' '/CONF_VERSION=/{$3=($3+1)"\x27"} 1' OFS=. abc.toml > abc.toml But it is not working. How can I update the file also and not printing on console?
@HarshitGoel I just added to the end of the answer two methods for updating the file instead of printing on the console.
After running this, I am getting an out put like this: Usage: awk [POSIX or GNU style options] -f progfile [--] file ... Usage: awk [POSIX or GNU style options] [--] 'program' file ... POSIX options: GNU long options: (standard) -f progfile --file=progfile gawk is a pattern scanning and processing language. By default it reads standard input and writes standard output.
@HarshitGoel Oops. I had a typo in the second version. Try the corrected version awk -F'[.]' '/CONF_VERSION=/{$3=($3+1)"\x27"} 1' OFS=. abc.toml >temp && mv temp
Thanks @John1024 That really helped :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.