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
Advertisement
Answer
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.