How to convert 2d array into html file in bash/shell? - html

I have a bash script in which I have my data in 2d array now currently I am printing this matrix as follows:
f1="%$((${#num_rows}+1))s"
f2=" %12s"
printf "$f1" ''
printf "$f2" "Total"
printf "$f2" "Pass"
printf "$f2" "Stillfail"
printf "$f2" "Failure"
printf "$f2" "APIName"
printf "$f2"
echo
for ((j=1;j<=num_rows;j++)) do
printf "$f1" $j
for ((i=1;i<=num_columns;i++)) do
if [ i == 5 ]; then
printf "%-20s" ${matrix[$j,$i]}
else
printf "$f2" ${matrix[$j,$i]}
done
echo
done
I tried a lot but still there is alignment problem so now I want to create a html table to output this data.Can anyone guide me on this? Thanks in advance

Here is the complete script. I think this will fix the issue
#!/bin/bash
declare -a matrix
num_rows=6
num_columns=5
for ((i=1;i<=num_columns;i++)) do
for ((j=1;j<=num_rows;j++)) do
matrix[$i,$j]=$RANDOM
done
done
f1="%$((${#num_columns}+3))s"
f2=" %12s"
header=( "" "Total" "Pass" "Stillfail" "Failure" "APIName" )
printf "$f1|" 'No.'
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "%-20s|" ${header[$i]}
else
printf "$f2|" ${header[$i]}
fi
done
echo
for ((j=1;j<=num_rows;j++)) do
printf "$f1|" $j
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "%-20s|" ${matrix[$j,$i]}
else
printf "$f2|" ${matrix[$j,$i]}
fi
done
echo
done
echo
echo "-----HTML----"
echo "<table style="width:50%">"
printf "$f1" '<tr><th>No.</th>'
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "<th align=left>%s</th>" ${header[$i]}
else
printf "<th align=right>%s</th>" ${header[$i]}
fi
done
echo '</tr>'
for ((j=1;j<=num_rows;j++)) do
printf "<tr><td align=right>$f1</td>" $j
for ((i=1;i<=num_columns;i++)) do
if [ $i -eq 3 ]; then
printf "<td align=left>%s</td>" ${matrix[$j,$i]}
else
printf "<td align=right>%s</td>" ${matrix[$i,$j]}
fi
done
echo '</tr>'
done
echo "</tr></table>"

Related

Bash shell script html css

while read row do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
When I execute this fraction of code it gives me this error:
./table_html.csv: line 36: syntax error near unexpected token `echo'
./table_html.csv: line 36: ` echo "<td>$valore</td>" >> $file'
How do I fix it?
I'm not very good with shell scripts, that's why I can not think of anything else.
(Ah I was working on this when the alarm for the oven went off) what I wanted to say was it's just the "do" in the wrong place after the while. So here's your code restated (with a file="filename" added to get it working):
file="test2.html"
while read row
do
echo "<tr>" >> $file
for valore in $row
do
echo "<td>$valore</td>" >> $file
done
echo "</tr>" >> $file
done < alunni.txt
echo "</table>" >> $file
So it was just a line break before the do is all you needed :-)

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.

How to put the command to remove duplicate lines in my awk script?

I create a CGI in bash/html.
My awk script looks like :
echo "<p><h2>FRAME : $test</h2></p>"
echo "<table>"
for fn in /var/www/cgi-bin/LPAR_MAP/*;
do
echo "<td>"
echo "<PRE>"
awk -F',|;' -v test="$test" '
NR==1 {
split(FILENAME ,a,"[-.]");
}
$0 ~ test {
if(!header++){
print "DATE ========================== : " a[4]
}
print ""
print "LPARS :" $2
print "RAM : " $5
print "CPU 1 : " $6
print "CPU 2 : " $7
print ""
print ""
}' $fn;
echo "</PRE>"
echo "</td>"
done
echo "</table>"
This script allow to analyze 276 csv files that looks like :
MO2PPC20;mo2vio20b;Running;VIOS 2.2.5.20;7;1.0;2;DefaultPool;shared;uncap;192
MO2PPC20;mo2vio20a;Running;VIOS 2.2.5.20;7;1.0;2;DefaultPool;shared;uncap;192
MO2PPC21;mplaix0311;Running;AIX 7.1 7100-05-02-1832;35;0.6;4;DefaultPool;shared;uncap;64
MO2PPC21;miaibv194;Running;AIX 6.1 6100-09-11-1810;11;0.2;1;DefaultPool;shared;uncap;64
MO2PPC21;mplaix0032;Running;AIX 6.1 6100-09-11-1810;105;4.0;11;DefaultPool;shared;uncap;128
MO2PPC21;mplaix0190;Running;Unknown;243;4.9;30;DefaultPool;shared;uncap;128
MO2PPC21;mo2vio21b;Running;VIOS 2.2.6.10;6;1.5;3;DefaultPool;shared;uncap;192
MO2PPC21;miaibv238;Running;AIX 7.1 7100-05-02-1810;10;0.5;1;DefaultPool;shared;uncap;64
MO2PPC21;mo2vio21a;Running;VIOS 2.2.6.10;6;1.5;3;DefaultPool;shared;uncap;192
MO2PPC21;miaibv193;Running;AIX 6.1 6100-09-11-1810;12;0.2;1;DefaultPool;shared;uncap;64
MO1PPC17;miaibe03;Running;AIX 5.2 5200-10-08-0930;25;null;3;null;ded;share_idle_procs;null
MO1PPC17;miaiba12;Running;AIX 5.2 5200-10-08-0930;17;null;2;null;ded;share_idle_procs;null
MO1PPC17;miaibf03;Running;AIX 5.2 5200-10-08-0930;30;null;3;null;ded;share_idle_procs;null
MO1PPC17;miaibc05;Running;AIX 5.2 5200-10-08-0930;40;null;2;null;ded;share_idle_procs;null
And to display them in my CGI like this :
The numbers of columns is equal at the number of csv to analyze.
As you can see in the screenshot, some lines are sometimes the same in each csv files
The idea is to delete the lines that are the same in all my csv files.
I know the awk command :
awk '!a[$0]++'
But this command needs a file to be achieved.
Do you think it's possible to put this command in my awk script ?
Read all files at once with awk and add condition :
awk -F',|;' -v test="$test" '
BEGIN{
print "<table>"
}
FNR==1 {
if(close_tag++){
print "</PRE>"
print "<td>"
}
print "<td>"
print "<PRE>"
split(FILENAME ,a,"[-.]");
}
($0 ~ test) && (!dup[$0]++) {
if(!header++){
print "DATE ========================== : " a[4]
}
print ""
print "LPARS :" $2
print "RAM : " $5
print "CPU 1 : " $6
print "CPU 2 : " $7
print ""
print ""
}
END{
print "</PRE>"
print "</td>"
print "</table>"
}' /var/www/cgi-bin/LPAR_MAP/*

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

Get return value of function in a loop - shell script

Can't seem to understand the output of the following code snippet. trying to print the function return value in a loop
contains () {
local e
for e in "${#:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
line="ayush"
line2="this is a line containing ayush"
contains $line $line2
echo $? #prints 0
for i in 1 2 3;do
contains "$line" "$line2"
echo $? #prints 1 everytime
done
#Ayush Goel
The Problem is here,
contains () {
local e
for e in "${#:2}"; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
line="ayush"
line2="this is a line containing ayush"
contains $line $line2
echo $? #prints 0
for i in 1 2 3;do
contains $line $line2 # <------------------ ignore ""
echo $? # Now it will print 0
done
Difference between $var and "$var" :
1) $var case
var="this is the line"
for i in $var; do
printf $i
done
here it will print
this is the line
means $var is expanded using space
2)"$var" case
var="this is the line"
for i in "$var"; do
printf $i
done
this will print
this
here "$var" will be considered as a single argument and it will take only one value from the list.