How to increase the number in several files if the number is not always the same?

I have several files containing this line

Release: X 

I want to increase X in all files.

If X were constant between files, I could loop around the bash script files and do ($ 1, containing the previous release number, and $ 2 new, i.e. $ 1 + 1):

 sed 's/Release: '$1'/Release: '$2'/' <$file >$file.new 

Now, how do I do if the release number is different between the files?

Can I deal with sed?

use another tool?

+4
source share
2 answers

Use awk - this is exactly the tool for this:

 awk '/Release: [0-9]+/ { printf "Release: %d\n", $2+1 }' < $file > $file.new 

Transfer:

  • Find lines containing "Release:" followed by one or more digits.
  • Print "Release:" followed by a number and a new line. The number is the second word in the input line (digits) plus 1.
+13
source

This single-line perl will do the same as awk script, but does not destroy the rest of the file or the rest of the lines containing the release.

  perl -pe "$_=~s/Release: (\d+)/'Release: '. ($1+1)/e;" < file > file.new 
+8
source

All Articles