Issue while using echo command

Hi,
I have below shell script..

$ cat /tmp/1.txt
NAME-HOST-APPLIED TIME-DATE
$ cat /tmp/1.sh
#!/bin/ksh
echo "<html><table border size=1>" > /tmp/mail.html
for i in `cat /tmp/1.txt`
do
a1=`echo $i |cut -d "-" -f1`
a2=`echo $i |cut -d "-" -f2`
a3=`echo $i |cut -d "-" -f3`
a4=`echo $i |cut -d "-" -f4`
echo "<tr><td>$a1</td><td>$a2</td><td>$a3</td><td>$a4</td></tr>" >> /tmp/mail.html
done
echo "</table></html>" >> /tmp/mail.html
$

When i execute the script /tmp/1.sh i am getting the output like below..

$ sh /tmp/1.sh
$ cat /tmp/mail.html
<html><table border size=1>
<tr><td>NAME</td><td>HOST</td><td>APPLIED</td><td></td></tr>
<tr><td>TIME</td><td>DATE</td><td></td><td></td></tr>
</table></html>
$

But i want the output like

<html><table border size=1>
<tr><td>NAME</td><td>HOST</td><td>APPLIED TIME</td><td>DATE</td></tr>
</table></html>

I am not getting the desired ouptput since "APPLIED TIME" have space in between words.

Please help how can i get desired ouptput... i.e.,

"<tr><td>NAME</td><td>HOST</td><td>APPLIED TIME</td><td>DATE</td></tr>"

Hi thomas,

Try this

awk 'BEGIN {FS="-";print "<html><table border size=1>"}{
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",$1,$2,$3,$4);
}
END {print "</table></html>"}' /tmp/1.txt >> /tmp/mail.html

Thanks kumar...

Its working fine...

But can i use my script to get the desired output by doing any change in that? i.e (i.e same flow and for loop)

Use a while loop instead of for loop. Set IFS to - and read fields into separate variables.

echo "<html><body>"
echo "<table border size=1>"

while IFS="-" read f1 f2 f3 f4
do
        echo "<tr>"
        echo "<td>${f1}</td>"
        echo "<td>${f2}</td>"
        echo "<td>${f3}</td>"
        echo "<td>${f4}</td>"
        echo "</tr>"
done < /tmp/1.txt

echo "</table>"
echo "</body></html>"

If output looks good, redirect it to another file.

Hi Thomas,
Yes..You can use n number of parameters,jus need to add only in the printf statements in terms of %s and $n.

regards,
kumar