1

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.

oguz ismail
  • 1
  • 16
  • 47
  • 69
  • 1
    Welcome to SO, please do add your efforts in form of code in your question, which is highly encouraged on SO, thank you. – RavinderSingh13 Mar 27 '21 at 15:48
  • 1
    please review [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and then come back and update your question accordingly; in particular ... provide a sample input file, the code you've tried so far, the (wrong) results of generated by your code, and the (correct) desired results; also consider running a google search on `increment a number in a file` and review the plethora of hits – markp-fuso Mar 27 '21 at 15:59
  • sed and awk are 2 completely different tools, there is no sed/awk just like there's no grep/sed or sort/awk, etc.. One of them, sed, is a stream editor used for doing s/old/new/ with no arithmetic capability and so considering using it to increment a value found in the input would be wrong. The other one, awk, does have arithmetic capability and is the mandatory POSIX tool present on every Unix box for general purpose text processing so considering using it to increment a value found in the input would be reasonable. – Ed Morton Mar 27 '21 at 20:49
  • @EdMorton thanks for the knowledge. I wasn't ware that sed did not have arithmetic capability. – a1i3n d1v13r Mar 30 '21 at 16:56

1 Answers1

5

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.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • An alternative syntax: `awk -v FS='=' -v OFS='=' '$1=="pkgrel"{$2++; print}' PKGBUILD` – Cyrus Mar 27 '21 at 16:46
  • Yes, it is a bug. It should be: `awk -v FS='=' -v OFS='=' '$1=="pkgrel"{$2++} {print}' PKGBUILD` – Cyrus Mar 27 '21 at 18:28