So I see a couple of problems, the biggest you have is that gawk does not make changes to a file and since you are appending all changes to the same test.out it will only ever get
longer with each growing change. Also, looking at your code I believe there are typos and missing pieces as what you have shown would never work (I refer here to the gawk line that
has no input file and the actions start with a single quote but end in a double??
That aside, I see at least 2 obvious solutions:
1. Do the whole thing in gawk and place your changes in their own file (ie the words to pass in earth and so on)
2. Use sed to make the changes to the original file on each iteration
My preference would be sed:
cp test.txt test.out
sed -i "s/cond$((i++))/$arg/g" test.out
Are you sure $args[i] should not read $args[$i]?
@jinkels - Yes the 'i' does not require quoting when used as an index for an array (numeric only of course)