[Solved] Shell script output in HTML or with table like results and mail

Hello,
Currently i have a script which will disply the results in plain text format.
I want to format the result in more readable format like Making bold headings and format with colors etc. Something like html and send that content as email.
Please help me how i can do that.

I am using below script for sending the content:

filesend=filename
cat mail_ids.txt |while read line
do
  elm -s "TEST" $line < $filesend
done 

you can try some thing like this

cat new
SERVER DB TABLE FREE SPACE
111.11.11.11 xyz ABCD 1000 YES
111.11.11.11 xyz ABCD 1000 YES
111.11.11.11 xyz ABCD 1000 YES
111.11.11.11 xyz ABCD 1000 YES
awk 'BEGIN { print "<table borader=1>"} {print "<tr>"; for(i=1;i<=NF;i++)print "<td><FONT COLOR=BLUE FACE="Geneva, Arial" SIZE=6>" $i"</FONT></td>";\
> print "</tr>"} END{print "</table>" }' new > temp.html

to email temp.html

(
echo "From: abcd@abcd.com "
echo "To: xyz@xyz.com"
echo "MIME-Version: 1.0"
echo "Subject: Test HTML e-mail."
echo "Content-Type: text/html"
cat $1
)|sendmail -t

Thanx for your reply.......but can you pls give me the both on into one script.

awk '
        BEGIN {
                print "From: from@domain.com"
                print "To: to@domain.com"
                print "MIME-Version: 1.0"
                print "Content-Type: text/html"
                print "Subject: Email Subject"
                print "<html><body>"
                print "<table border=1 cellspacing=2 cellpadding=2>"
        }
        !/^#/ && /^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                        print "<td><b>" $i "<b></td>"
                print "</tr>"
        }
        !/^#/ && !/^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                        print "<td>" $i "</td>"
                print "</tr>"
        }
        END {
                print "</table></body></html>"
        }
' inputfile | /usr/sbin/sendmail -t

Change the formatting as per your needs.

1 Like

Thnax yoda,

i hv used this code and its working fine.

but i am facing lil issue with merging the cells with same string.
means name "111.11.11.11" should be only once in mail with merged cell.

looking forward for your support.

---------- Post updated at 05:13 AM ---------- Previous update was at 02:10 AM ----------

can anyone help me out.........

Do not bump up questions if they are not answered promptly.

Assuming first field is sorted, you could read the input file twice and use rowspan attribute to merge cells:

awk '
        BEGIN {
                print "From: from@domain.com"
                print "To: to@domain.com"
                print "MIME-Version: 1.0"
                print "Content-Type: text/html"
                print "Subject: Email Subject"
                print "<html><body>"
                print "<table border=1 cellspacing=2 cellpadding=2>"
        }
        NR == FNR {
                if ( $0 !~ /^#/ && $0 !~ /^S/ )
                        C[$1]++
                next
        }
        !/^#/ && /^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                        print "<td><b>" $i "</b></td>"
                print "</tr>"
        }
        !/^#/ && !/^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i == 1 && !( $i in R ) )
                        {
                                print "<td rowspan=" C[$i] ">" $i "</td>"
                                R[$i]
                        }
                        if ( i > 1 )
                                 print "<td>" $i "</td>"
                }
                print "</tr>"
        }
        END {
                print "</table></body></html>"
        }
' inputfile inputfile | /usr/sbin/sendmail -t
1 Like

thanx for reply.....

i made script as per above:
value which is more than 80 will come in RED color.

##################################

        NR == FNR {
                if ( $0 !~ /^#/ && $0 !~ /^S/ )
                        C[$1]++
                next
        }
        !/^#/ && /^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                        print "<td><b>" $i "</b></td>"
                print "</tr>"
        }
        !/^#/ && !/^S/ {
                print "<tr>"
                for ( i = 1; i <= NF; i++ )
                {
                        if ( i == 1 && !( $i in R ) )
                        {
                                print "<td rowspan=" C[$i] ">" $i "</td>"
                                R[$i]
                        }
                        if ( i > 1 )
                        if ($3 >=80 )
                                   print "<td> <FONT COLOR=RED FACE="Geneva,Arial"SIZE=9 >"  $i  "</td>"
                        else
                                   print "<td> <FONT COLOR=BLACK FACE="Geneva,Arial"SIZE=9 >"  $i  "</td>"

                }
                print "</tr>"
        }
        END {
                print "</table></body></html>"
        }

' file file

######################################

but for every line which contains numeric value "9" in third field is coming in red colour.

THANX.

If the expression $3 >=80 is evaluating to TRUE when the 3rd field contains the value 9, awk must be performing a string comparison instead of a numeric comparison. Try changing:

                        if ($3 >=80 )

to:

                        if (($3 + 0) >=80 )

Of course, we haven't seen any sample input in this thread. It might help if we could see an input sample that is leading to these unexpected results.

1 Like

Thanx a lot,
its working :slight_smile:

hi,

facing issue while i am passing a file to this code and when its converting and merging the 1st column , when its come to string starting with "S" (capital S) on 1st column.

        NR == FNR {
                if ( $0 !~ /^#/ && $0 !~ /^S/ )
                        C[$1]++
                next

Please help.

When you have a new problem, please start a new thread.

When you give us four lines out of an awk script with no indication of what you're trying to do and no indication of what you think these lines are doing wrong, there isn't much we can do to help.

Please start a new thread. In that thread, show us your complete awk script, sample input to the script, corresponding sample output you want from the script, and an explanation of what transformations are supposed to be applied.