Shell script output in tabular format - html

I have an output from my script -
$cat file.txt
Jobname Date Time Status
abc 12/9/11 17:00 Completed
I have used the below code to get the output in tabular format
awk 'BEGIN{print ""} {print "";for(i=1;i<=NF;i++)print "" $i"";print ""} END{print ""}' file.txt > file.html
$cat file.html
And then I am sending this to my mail
(
echo "From: XXXX#oooo.com "
echo "To: bbbb#oooo.com "
echo "MIME-Version: 1.0"
echo "Subject: Test HTML e-mail."
echo "Content-Type: text/html"
cat file.html
) | sendmail -t
I am getting the output in my mail in this format
Whereas I am expecting something like -

you should add a border to your table, eg :
<style>
table, th, td {
border: 1px solid black;
}
</style>

Related

How to display Html Tabular Data in email Body using Mailx or Mutt

I have a Scenario where i am having one .csv file called Studentdetails.csv
The student details has below following data
Ram,Mumbai,MBA
Viraj,Delhi,MCA
Vilas,Kolkata,MMS
Priya,Agra,BCA
My below code convert .csv file to html table format file
awk 'BEGIN{
FS=","
print "MIME-Version: 1.0"
print "Content-Type: text/html"
print "Content-Disposition: inline"
print "<HTML>""<TABLE border="1"><TH>Name</TH><TH>City</TH><TH>Course</TH>"
}
{
printf "<TR>"
for(i=1;i<=NF;i++)
printf "<TD>%s</TD>", $i
print "</TR>"
}
END{
print "</TABLE></BODY></HTML>"
}
' file-to-convert.csv > StudentDetails.html
But my issue is How do i Display this HTML Tabular format in Body of mail
using mutt or mailx command
Note: Not as attachment
Output should be display in mail like below html tabular format

convert text file to html ouput so that data can be printed in table format

I have a text file which has 3 values separated by :
25-08-2019_19.00.00 : Port port1 of URL http://ip1:port1/ is NOT OPEN : Zoom1
25-08-2019_19.00.00 : Port port2 of URL http://ip2:port2/ is NOT OPEN : MP
and so on.
I want to print the output to a html type tabular format file, which has 3 headings, date, output and system and corresponding data in 3 columns.
I tried below code, but it is not putting data to table format.
#! /bin/bash
if [ $# -eq 0 ] ; then
echo "USAGE: $(basename $0) file1 file2 file3 ..."
exit 1
fi
for file in $* ; do
html=$(echo $file | sed 's/\.txt$/\.html/i')
echo "<html>" >> $html
echo "<style type="text/css">
table, th, td {
border: 1px solid black;
}
</style>" >> $html
echo " <body>" >> $html
echo '<table>' >> $htm
echo '<th>HEADING1</th>' >> $html
echo '<th>HEADING2</th>' >> $html
echo '<th>HEADING3</th>' >> $html
while IFS=':' read -ra line ; do
echo "<tr>" >> $html
for i in "${line[#]}"; do
echo "<td>$i</td>" >> $html
# echo "<td>$i</td>" >> $html
done
echo "</tr>" >> $html
done < $file
echo '</table>'
echo " </body>" >> $html
echo "</html>" >> $html
done
If you can, please consider awk:
awk -F' : ' '
BEGIN{
print "<html><style type=\"text/css\">table, th, td {border: 1px solid black;}</style><body><table><th>HEADING1</th><th>HEADING2</th><th>HEADING3</th>"
}
{
print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i "</td>"
print "</tr>"
}
END{
print "</table></body></html>"
}
' input_file > output.html
The BEGIN and END statement fills the static html tags.
The middle statement fills one line of the array according to each input file line by adding <tr> for each line and <td> for each field.

unix sed and awk commands get records and display html table

Am new to unix i tried to get users and their directory details using sed and awk commands and tried to display it in html table
I have tried the following code it can able to print one column of the table,I can't able to create 2nd and 3rd column kindly tell me the possible ways to print the records in the2nd and 3rd column
vars=`cat /etc/passwd | sed -e "s/:/\t/g" | awk '{print $1}'`
echo '<table style="width:100%" border="2px"><tr><th>username</th><th>column1</th><th>column2</th>' >> demo.html
for variable in $vars
do
echo "<tr><td>$variable</td></tr>" >> demo.html
done
echo "</table>" >> demo.html
This will generate a table which looks like the image below :
awk -F':' 'BEGIN{print "<table border=1 cellpadding=1 cellspacing=0 bordercolor=BLACK ><tr><td>User Name</td><td>Default Shell</td></tr>"}{ print "<tr><td>"$1"</td><td>"$(NF)"</td></tr>"} END { print "</table>" } ' /etc/passwd
Output
Assuming that the only the first columns might contain whitespace and the 2nd and 3rd will not,
then here's one way to write such script:
{
echo '<table style="width:100%" border="2px"><tr><th>username</th><th>column1</th><th>column2</th>'
awk -F: '{print $1; print $2, $3}' /etc/passwd | while read user
do
read pass uid
echo "<tr><td>$user</td><td>$pass</td><td>$uid</td></tr>"
done
echo "</table>"
} | tee demo.html
Here is one easy way using awk:
awk -F':' '
function wrap(tag,x) {return "<"tag">"x"</"tag">"}
function row(x) {return wrap("tr",x)}
function cell(x) {return wrap("td",x)}
BEGIN {
print "<table style=\"width:100%\" border=\"2px\">"
print row(cell("user_name") cell("home_dir"))
}
{
print row(cell($1) cell($6) )
}
END{
print "</table>"
}
' /etc/passwd
Would Output (with few records):
<table style="width:100%" border="2px">
<tr><td>user_name</td><td>home_dir</td></tr>
<tr><td>root</td><td>/root</td></tr>
<tr><td>daemon</td><td>/usr/sbin</td></tr>
<tr><td>bin</td><td>/bin</td></tr>
<tr><td>sys</td><td>/dev</td></tr>
<tr><td>sync</td><td>/bin</td></tr>
<tr><td>akshay</td><td>/home/akshay</td></tr>
<tr><td>mysql</td><td>/nonexistent</td></tr>
<tr><td>gdm</td><td>/var/lib/gdm3</td></tr>
</table>

How to send a html file using mails in shell script?

I want to store my matrix's data in a html file and send a email in outlook ,my code looks as follows:
printf "<!DOCTYPE html>"
printf "<html>"
printf "<head>"
printf "<style>"
printf "</style>"
printf "</head>"
printf "<body>"
printf "<table>"
printf "<tr>"
printf "<th>Total</th>"
printf "<th>StillFail</th>"
printf "<th>Pass</th>"
printf "<th>ScriptError</th>"
printf "<th>APIName</th>"
printf "</tr>"
printf "<tr>"
echo
for ((j=1;j<=num_rows;j++)) do
printf "<tr>"
for ((i=1;i<=num_columns;i++)) do
printf "<td>"
printf "${matrix[$j,$i]}"
printf "</td>"
printf "</tr>"
done
echo
done
printf "</tr>"
printf "</table>"
printf "</body>"
printf "</html>"
#mailx -a 'Content-Type: html' -s "my subject" test#example.com < output.html
mailx -s "TESTING MAIL"</home/test/example/output.html "test#example.com"
I want my output as a well aligned table. Can someone help me on this? TIA
You need to append content-type in header it can be done with -a flag in mailx
comment your last line and try this
mailx -a 'Content-Type: text/html' -s "Test MAIL" test#example.com</home/test/example/output.html
Edit :
As per the OP End error : content-type:texthtml no such file or
directory
To install mailutils in mac
Press Command+Space and type Terminal and press enter/return key.
Run in Terminal app:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null
and press enter/return key. Wait for the command to finish.
Run:
brew install mailutils
reference : http://macappstore.org/mailutils/
Maybe you can use this for a base:
(
echo "To: ADRESSES"
echo "Subject: SUBJECT"
echo "Content-Type: text/html"
echo
echo "HTML code"
) | /usr/sbin/sendmail -F "NameOfSender" -t

Creating an HTML table with BASH & AWK

I am having issues creating a html table to display stats from a text file. I am sure there are 100 ways to do this better but here it is:
(The comments in the following script show the outputs)
#!/bin/bash
function getapistats () {
curl -s http://api.example.com/stats > api-stats.txt
awk {'print $1'} api-stats.txt > api-stats-int.txt
awk {'print $2'} api-stats.txt > api-stats-fqdm.txt
}
# api-stats.txt example
# 992 cdn.example.com
# 227 static.foo.com
# 225 imgcdn.bar.com
# end api-stats.txt example
function get_int () {
for i in `cat api-stats-int.txt`;
do echo -e "<tr><td>${i}</td>";
done
}
function get_fqdn () {
for f in `cat api-stats-fqdn.txt`;
do echo -e "<td>${f}</td></tr>";
done
}
function build_table () {
echo "<table>";
echo -e "`get_int`" "`get_fqdn`";
#echo -e "`get_fqdn`";
echo "</table>";
}
getapistats;
build_table > api-stats.html;
# Output fail :|
# <table>
# <tr><td>992</td>
# <tr><td>227</td>
# <tr><td>225</td><td>cdn.example.com</td></tr>
# <td>static.foo.com</td></tr>
# <td>imgcdn.bar.com</td></tr>
# Desired output:
# <tr><td>992</td><td>cdn.example.com</td></tr>
# ...
This is reasonably simple to do in pure awk:
curl -s http://api.example.com/stats > api-stats.txt
awk 'BEGIN { print "<table>" }
{ print "<tr><td>" $1 "</td><td>" $2 "</td></tr>" }
END { print "</table>" }' api-stats.txt > api-stats.html
Awk is really made for this type of use.
You can do it with one awk at least.
curl -s http://api.example.com/stats | awk '
BEGIN{print "<table>"}
{printf("<tr><td>%d</td><td>%s</td></tr>\n",$1,$2)}
END{print "</table>"}
'
this can be done w/ bash ;)
while read -u 3 a && read -u 4 b;do
echo $a$b;
done 3&lt/etc/passwd 4&lt/etc/services
but my experience is that usually it's a bad thing to do things like this in bash/awk/etc
the feature i used in the code is deeply burried in the bash manual page...
i would recommend to use some real language for this kind of data processing for example: (ruby or python) because they are more flexible/readable/maintainable