Option 1: Using column
to format your existing code's output
Use column tool to format the code for you
$ cat test.sh
#!/bin/bash
pre="<tr><td>"
post="<\/td><\/tr>"
mid="<\/td><td>"
cat myfile.html | grep "<td>" | sed -e "s/^$pre//g;s/$post$//g;s/$mid/ /g" | awk '{ sum=($2+$3+$4); printf $1 " %.0f \n" ,sum}'
$ cat myfile.html
<table>
<tr><th >Country Name</th><th >City1</th><th >City2</th><th>City3</th></tr>
<tr><td>CHINA</td><td>500</td><td>700</td><td>1200</td></tr>
<tr><td>USA</td><td>400</td><td>600</td><td>1000</td></tr>
</table>
$ ./test.sh | column -t
CHINA 2400
USA 2000
Option 2: Updating your existing code's use of printf
If we know the longest possible country-name length, we can tell printf
to pad to it. Changing only the awk part of your existing answer (in this case, telling it to pad to 8 spaces):
grep "<td>" myfile.html \
| sed -e "s/^$pre//g;s/$post$//g;s/$mid/ /g" \
| awk '{ sum=($2+$3+$4); printf "%-08s %.0f \n", $1, sum}'
...we get output:
CHINA 2400
USA 2000