Incremental logic


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Incremental logic
# 15  
Old 01-22-2017
Reading threads on this site, you can learn various ways to perform lots of different tasks. If a script is given that does something you want to do and you don't understand what the script is doing, look at the man pages on your system for the commands that you don't understand and see if you can figure out how that script works. If you can't, add a post to that thread asking for an explanation of what that command is doing.

Note that some utilities behave differently on different operating systems (and even on different releases of the same operating system). So it may be that some of the examples given will work as described on the system(s) on which they were tested, but will not work on your system.

Note that back in post #5 I asked you what operating system and shell you're using. In post #10 you finally told us that you're using bash as your shell, but you still haven't told us what operating system you're using. Giving us this information in the first post of a thread makes it much more likely that you'll get suggestions that help you do what you want to do much faster and greatly reduce the number of suggestions provided that will work on some systems, but will not work on your system. The more details you can give us about what you are trying to do, what you have tried, what is and/or is not working, will help us help you learn about what options you have to do what you're trying to do. The fewer details you give, the more wild guesses we have to make and it becomes more likely that suggestions provided won't do what you're really trying to do.

Making lots of wild guesses on what you are trying to do from what has been said in this thread, I might guess that you have a script that wants to read a file named pom.xml, find the version string in that file, update that version string and save that updated value in a variable named b and in the file pom.xml. If that is what you're trying to do, you might want to consider the following script as an alternative to some of the suggestions that have been provided so far (which only update a version number and save the updated value in a variable named b). (Note that I generally use ksh instead of bash because I sometimes like to perform floating point calculations with arithmetic expansions, I like that the last element on a pipeline runs in the current shell execution environment, and frequently runs a little faster; but the following script will work with no changes whether it is run by bash or ksh.)

The following script takes one operand naming a file to be processed. (If no operand is supplied, it defaults to using pom.xml.) It contains an awk script that reads that file, looks for version strings, and produces two outputs. One output is a new file with the name of the input file with .new appended that contains the text from the input file with updated version strings. The other output is a list of updated version strings, one per line, written to its standard output. This script prints the contents of the input file, calls the awk script (saving the output in a variable named b), prints the contents of that variable, and then prints the contents of the updated file produced by the awk script:
Code:
#!/bin/ksh
file=${1:-pom.xml}
printf '"%s" contains:\n' "$file"
cat "$file"
b=$(awk '
match($0, /[[]project-SNAPSHOT-([0-9][0-9][.]){3}[0-9][0-9]]/) {
	vn = substr($0, RSTART, RLENGTH)
	gsub(/[^0-9]/, "", vn)
	vn = ("1" vn) + 1
	vn = substr(vn, 2, 2) "." substr(vn, 4, 2) "." \
	     substr(vn, 6, 2) "." substr(vn, 8, 2)
	print substr($0, 1, RSTART + 17) vn \
	      substr($0, RSTART + RLENGTH - 1) > (FILENAME ".new")
	print vn
	next
}
{	print > (FILENAME ".new")
}' "$file")

printf '\n\nb has been set to "%s"\n\n"%s.new" contains:\n' "$b" "$file"
cat "$file.new"

If you want to try this script on a Solaris/SunOS system, change awk in the script to /usr/xpg4/bin/awk or nawk.

I created two sample input files, one named pom.xml containing just one version string (which I assume is what you will be doing most of the time) and one named ultiverions.xml containing several version strings making it easier to verify that the code is working as expected without creating lots of test files.

If the above script is named tester and made executable, the command:
Code:
./tester

and the command:
Code:
./tester pom.xml

produce the output:
Code:
"pom.xml" contains:
<xml>
<version>[project-SNAPSHOT-96.99.99.99]</version>
<tag>... ... ...</tag>
<more_tags>
...
...
...
</more_tags>
</xml>


b has been set to "97.00.00.00"

"pom.xml.new" contains:
<xml>
<version>[project-SNAPSHOT-97.00.00.00]</version>
<tag>... ... ...</tag>
<more_tags>
...
...
...
</more_tags>
</xml>

and the command:
Code:
./tester multiverions.xml

produces the output:
Code:
"multiverions.xml" contains:
am reading it from pom.xml, lets say original one is [project-SNAPSHOT-00.00.00.01]
And i should change that to [project-SNAPSHOT-00.00.00.02] push back git
a line with no snapshot
3rd snapshot rollover to 00.01.00.00 [project-SNAPSHOT-00.00.99.99] after
4th snapshot rollover to 99.00.00.00 [project-SNAPSHOT-98.99.99.99] after
5th snapshot rollover to 00.00.00.00 [project-SNAPSHOT-99.99.99.99] after


b has been set to "00.00.00.02
00.00.00.03
00.01.00.00
99.00.00.00
00.00.00.00"

"multiverions.xml.new" contains:
am reading it from pom.xml, lets say original one is [project-SNAPSHOT-00.00.00.02]
And i should change that to [project-SNAPSHOT-00.00.00.03] push back git
a line with no snapshot
3rd snapshot rollover to 00.01.00.00 [project-SNAPSHOT-00.01.00.00] after
4th snapshot rollover to 99.00.00.00 [project-SNAPSHOT-99.00.00.00] after
5th snapshot rollover to 00.00.00.00 [project-SNAPSHOT-00.00.00.00] after

Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

incremental by 1

let says, i have this number as 000002080, i want to add 1 to make it 000002081, and then i want to add 1 to 000002082, add 1 to 000002083, 84. i=000002080 TOT=$(echo "scale=9; $i + 1" | bc) echo $TOT it shows 2081, i want to retain 000002081, 000002082, 000002082, 000002084. (2 Replies)
Discussion started by: tjmannonline
2 Replies
Login or Register to Ask a Question