awksedarchlinuxpkgbuild

Use SED/AWK to increment value of a variable in a PKGBUILD file


I have a PKGBUILD file (for AUR) which I am maintaining. It has a value 'pkgrel' which I increment by 1 (manually), before building the package each time.

To summarize, if I want to build the package the sixth time, I'll manually set the value as

pkgrel=6

There are other processes for which I have written a bash script. I am stuck here. Please guide me on how to increment that value of pkgrel via any sed/awk expression. I want it that way so that say, I build the package the 7th time, the SED/AWK expression should set the value to 7

pkgrel=7

before I run the makepkg command in my script.

I am not restricting answers to sed/awk, if any other method works, please do guide me. The only condition is it should be bash.


Solution

  • Assuming pkgrel is the first word in its line and there are no spaces around it or around the =, you can use the following awk command to print an updated PKGBUILD.

    awk -F= -v OFS== '$1=="pkgrel" {$2++} 1' PKGBUILD 
    

    To update the file in-place you can use GNU awk's -i inplace option.