Although it would certainly be possible to modify your existing code to accomplish this task, I chose to refactor it instead using methods I am a bit more familiar with than awk.
An added feature of the method I used is that an empty cell will be automatically added for players with an odd number of teams.
First, from your example, I assume you would have a file named "Player 1teams.list" with the follow text:
Team 1
Team 2
Team 3
Team 4
Here is my proposed solution, commented and optimized for readability:
#!/bin/bash
# Array of players
PLAYERS=("Player 1")
# Loop through players array
for PLAYER in "${PLAYERS[@]}"; do
# Echo the table header row for the current player
echo "<tr>"
echo " <th colspan=2>${PLAYER}</th>"
echo "</tr>"
# Reset the TEAMS array on each loop (reuse the array for each player)
TEAMS=()
# Read each line of the teams file for the current player
while IFS= read -r LINE || [[ -n "$LINE" ]]; do
# Store the current player's teams in an array
TEAMS+=("$LINE")
done < "${PLAYER}teams.list"
# Calculate the array key for the last line of the file
# (array keys are used for line numbers, so they start at 0 instead of 1)
LASTLINE="$(( ${#TEAMS[@]} - 1 ))"
# Loop through teams for the current player
for LINE in "${!TEAMS[@]}"; do
# If the current line number is even, we are starting a table row
if [ "$(( LINE % 2 ))" -eq 0 ]; then
echo "<tr>"
echo " <td>${TEAMS[$LINE]}</td>"
# If an even line is the last line of the file, add a blank cell
if [ "$LINE" == "$LASTLINE" ]; then
echo " <td></td>"
echo "</tr>"
fi
# If the current line number is odd, we are ending a table row
else
echo " <td>${TEAMS[$LINE]}</td>"
echo "</tr>"
fi
done
done
Here is a condensed version of the the above code:
#!/bin/bash
# Array of players
PLAYERS=("Player 1")
for PLAYER in "${PLAYERS[@]}"; do
echo -e "<tr>\n <th colspan=2>${PLAYER}</th>\n</tr>"
TEAMS=(); while IFS= read -r LINE || [[ -n "$LINE" ]]; do TEAMS+=("$LINE"); done < "${PLAYER}teams.list"
LASTLINE="$(( ${#TEAMS[@]} - 1 ))"
for LINE in "${!TEAMS[@]}"; do
if [ "$(( LINE % 2 ))" -eq 0 ]; then
echo -e "<tr>\n <td>${TEAMS[$LINE]}</td>"
if [ "$LINE" == "$LASTLINE" ]; then echo -e " <td></td>\n</tr>"; fi
else
echo -e " <td>${TEAMS[$LINE]}</td>\n</tr>"
fi
done
done
Edit: I have posted a new answer using the awk code you provided as well
$(j+1)for the 2nd value and increment loop by2? Good luck.