HTML formatting in shellscript

I have written one script which connects to MYSQL database, fires one select query and send the results over an email, if there is any Output.

But the Output which I am receiving over email is in text format. I need to get it dispalyed in tabular format to ensure better readability. Below is the script I have tried and the sample email what I am receiving:

#!/bin/bash
#MAILTO="sambit.sahu@niceactimize.com"
mysql test -e "select c.CenterID,a.MIC,c.ExchangeName,a.CountryCode,a.EventDate,a.EventDayOfWeek,b.eventName from exch_calendar_event a left join exch_calendar_eventName b on a.EventID=b.ID left join exch_calendar_mic c on a.MIC=c.MIC where EventDate=DATE_FORMAT(20170811,'%Y%m%d') order by a.MIC;" > file.tmp
if [ -s file.tmp ]
then
       mailx -s "Holiday" $MAILTO  <file.tmp
fi
rm file.tmp

Below is the output, what I am receiving over mail. I want to convert it into tabular format with adding HTML formatting.

CenterID    MIC    ExchangeName    CountryCode    EventDate    EventDayOfWeek    eventName
706    XTKS    Tokyo Stock Exchange    JP    20170811    Fri    Mountain Day

Welcome to the forum.

Your request has been covered umpteen times in these forums; did you bother to search? Try this as a starting point but make sure fields are separated by a <TAB> char if some of them can contain spaces:

awk -F"\t"      '
BEGIN   {printf "<html><body><table border=\"1\">\n"}

        {printf "<tr>"; for (i=1; i<=NF; i++) printf "<td>%s</td>", $i; printf "</tr>\n" }

 END    {printf "</table></body></html>\n"}
' file
1 Like

Thanks Rudi, but , please can you let me know where to place this formatting in the code. I am not getting the desired output.

Between file creation and mailing it out. Make sure to insert the actual file name.