awk syntax mistake doubles desired output

I am trying to add a line to a BASH shell script to print out a large variable length table on a web page. I am very new to this obviously, but I tried this with awk and it prints out every line twice. What I am doing wrong?

echo "1^2^3%4^5^6%7^8^9%" | awk 'BEGIN { RS="%"; FS="^"; } {for (i = 1; i<NF; i++)printf("<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>", $1,$2,$3,$1)}'

I included this hard coded text string as an example of the formatted variable data as input to awk. When I test from the command line or in the script it prints out 2 of each output line like this:

<tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1></td></tr><tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1></td></tr><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=4></td></tr><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=4></td></tr><tr><td>7</td><td>8</td><td>$9</td><td align=center><input type=checkbox name=DA value=7></td></tr><tr><td>7</td><td>8</td><td>$9</td><td align=center><input type=checkbox name=DA value=7></td></tr>

Also for some reason on my system NF returns 1 more than the actual number of records every time I have ever used awk, so that is why I have < rather than <= on this for loop.

TIA for helping someone without a clue.

echo "1^2^3%4^5^6%7^8^9" | \
awk 'BEGIN { RS="%"; FS="^"; } {for(i=1;i<=NF;i+=3)printf "<tr><td>%d</td><td>%d</td><td>$%d</td><td align=center><input type=checkbox name=DA value=1>", $1,$2,$3}'

Output:

<tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=1><tr><td>7</td><td>8</td><td>$9</td><td align=center><input type=checkbox name=DA value=1>

Feel like an idiot on this one. I actually used to program in C quite a bit, but maybe several days without sleep contributed to my incompetence. I can't believe I was only incrementing by 1 on that loop processing 3 fields!! THANKS for the immediate reply.

NOTE: Still have to use < instead of <= for NF or I get an extra line with no variables, but that is always the case on my system for some reason.

This is because there is an extra %-sign at the end of the string you are echoing. The 3 %-signs make 4 records, the last one empty...

Even more of an idiot than I thought. I have been appending all my record separators instead of prepending. Man, this is the place to come for quick replies. Thanks again.

Actually you do not need the for loop:

printf "%s" "1^2^3%4^5^6%7^8^9%" |
awk '{printf("<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>", $1,$2,$3,$1)}' RS=% FS=^

Alternatively, since you are using bash, it could also be done without awk:

printf "%s" "1^2^3%4^5^6%7^8^9%" | tr % '\n' |
while IFS=^ read f1 f2 f3
do
  printf "<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>" "$f1" "$f2" "$f3" "$f1"
done

or in bash code:

while IFS=^ read -d% f1 f2 f3
do
  printf "<tr><td>%s</td><td>%s</td><td>\$%s</td><td align=center><input type=checkbox name=DA value=%s></td></tr>" "$f1" "$f2" "$f3" "$f1"
done <<< "1^2^3%4^5^6%7^8^9%"

Just tried the prepending record separator with the <= and also your latest awk example with no loop and got this output both times:

<tr><td></td><td></td><td>$</td><td align=center><input type=checkbox name=DA value=></td></tr><tr><td>1</td><td>2</td><td>$3</td><td align=center><input type=checkbox name=DA value=1></td></tr><tr><td>4</td><td>5</td><td>$6</td><td align=center><input type=checkbox name=DA value=4></td></tr><tr><td>7</td><td>8</td><td>$9

It works if I loop and increment the counter by 3 and use < instead of <= on NF with the trailing % separator for each record. I probably need to stop posting until I get some sleep as my mistakes seem to be compounding themselves at this point. Was trying to meet a deadline. Great help. I'll check back when I am lucid.