Anytime you reset a numbered field (such as $2=x or sub(regex, replacement, $n) ) for n not equal to 0), $0 is recalculated. For the general case, you could try something like:
If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
The loop printing the modified lines and the following print command can be simplified to a single printf command if you know that there are always three fields.