Loop to convert text output in the HTML format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop to convert text output in the HTML format
# 1  
Old 01-11-2016
Loop to convert text output in the HTML format

Hello Everyone,

I have a sample file raw.txt as shown below :

Code:
Drive Bays

      Bay Name                             :    SD-2C
      Number of Standby Power Supplies     :    4
      Number of Drive Enclosures           :    12

      Summary Status of Contained Modules
        All Enclosures                     :    Normal
        All Link Control Cards             :    Normal
        All Power Supplies                 :    Normal
        All Standby Power Supplies         :    Normal

      Bay Name                             :    SD-2D
      Number of Standby Power Supplies     :    3
      Number of Drive Enclosures           :    16

      Summary Status of Contained Modules
        All Enclosures                     :    Normal
        All Link Control Cards             :    Normal
        All Power Supplies                 :    Normal
        All Standby Power Supplies         :    Normal

      Bay Name                             :    SD-2F
      Number of Standby Power Supplies     :    3
      Number of Drive Enclosures           :    12

      Summary Status of Contained Modules
        All Enclosures                     :    Normal
        All Link Control Cards             :    Normal
        All Power Supplies                 :    Normal
        All Standby Power Supplies         :    Normal

      Bay Name                             :    SD-2G
      Number of Standby Power Supplies     :    6

I want to check for the number of fields ( Field separater is ":") and convert the file contents in HTML format .If there is 1 field (no ":" sign is present for example in case of Drive bays from the file) then
Code:
echo "<tr><td>$arr[0]</td></tr>"

should be printed
where arr[0] is the first field and in case of two fields (for example in case of Bay Name : SD-2C) following should be printed :
Code:
echo "<tr><td>$arr[0]</td><td>$arr[1]</td></tr>"



I tried following to get the number of fields but I am stuck and not getting correct number of fields. Also I got correct number of fields without using :
read -a arr IFS=':' but I have to use this code to create array

Code:
while read line
do
read -a arr IFS=':'
count=`echo "$line" | awk -F":" '{print NF}'`
echo $count
done < raw.txt

Please help

# 2  
Old 01-11-2016
Hello rahul2662,

Could you please try following and let me know if this helps you, not tested though. Please let me know if you have any queries on same.
Code:
awk -F"[[:space:]]+:[[:space:]]+" 'NF>1{sub(/^[[:space:]]+/,X,$0);for(i=1;i<=NF;i++){Q=Q?Q OFS "<td>" $i "</td>":"<td>" $i "</td>"};print "<tr>" Q "</tr>";Q="";next} NF==1{sub(/^[[:space:]]+/,X,$0);print "<tr><td>"$0"</td></tr>"}' OFS=""   Input_file

Output will be as follows.
Code:
<tr><td>Drive Bays</td></tr>
<tr><td>Bay Name</td><td>SD-2C</td></tr>
<tr><td>Number of Standby Power Supplies</td><td>4</td></tr>
<tr><td>Number of Drive Enclosures</td><td>12</td></tr>
<tr><td>Summary Status of Contained Modules</td></tr>
<tr><td>All Enclosures</td><td>Normal</td></tr>
<tr><td>All Link Control Cards</td><td>Normal</td></tr>
<tr><td>All Power Supplies</td><td>Normal</td></tr>
<tr><td>All Standby Power Supplies</td><td>Normal</td></tr>
<tr><td>Bay Name</td><td>SD-2D</td></tr>
<tr><td>Number of Standby Power Supplies</td><td>3</td></tr>
<tr><td>Number of Drive Enclosures</td><td>16</td></tr>
<tr><td>Summary Status of Contained Modules</td></tr>
<tr><td>All Enclosures</td><td>Normal</td></tr>
<tr><td>All Link Control Cards</td><td>Normal</td></tr>
<tr><td>All Power Supplies</td><td>Normal</td></tr>
<tr><td>All Standby Power Supplies</td><td>Normal</td></tr>
<tr><td>Bay Name</td><td>SD-2F</td></tr>
<tr><td>Number of Standby Power Supplies</td><td>3</td></tr>
<tr><td>Number of Drive Enclosures</td><td>12</td></tr>
<tr><td>Summary Status of Contained Modules</td></tr>
<tr><td>All Enclosures</td><td>Normal</td></tr>
<tr><td>All Link Control Cards</td><td>Normal</td></tr>
<tr><td>All Power Supplies</td><td>Normal</td></tr>
<tr><td>All Standby Power Supplies</td><td>Normal</td></tr>
<tr><td>Bay Name</td><td>SD-2G</td></tr>
<tr><td>Number of Standby Power Supplies</td><td>6</td></tr>

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 01-11-2016
Try also
Code:
awk     'BEGIN  {printf "<html><body><table width=\"600\" border=\"3\" align=\"center\">\n"}
                {gsub (/  +/, _)
                 printf "<tr>"
                 for (i=1; i<=NF; i++) printf "<td>%s</td>", $i
                 printf "</tr>\n"
                }
         END    {printf "</table></body></html>\n"}
        ' FS=: file

---------- Post updated at 10:39 ---------- Previous update was at 10:29 ----------

Try also this adaption to your shell approach:
Code:
while read line; do echo "<tr><td>${line//:/<\/td><td>}</td></tr>"; done <file


Last edited by RudiC; 01-11-2016 at 07:09 AM.. Reason: typo
This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-11-2016
Thanks a lot Ravinder ,

Also I need one small change in the Output.

In case of Drive bays and Summary Status of Contained Modules we need to have the following column : Value as well and output should look like below

<tr><td>Drive Bays</td><td>Value</td></td>
<tr><td>Summary Status of Contained Modules</td><td>Value</td></tr>

Could you also please explain me the code.

Thanks
Rahul

Edit : I got the value (column) as well after adding making small change in the code
# 5  
Old 01-11-2016
Hello rahul2662,

Following is the explanation for same which may help you, let me know if you have any queries on same.
Code:
awk -F"[[:space:]]+:[[:space:]]+"                 ####### Making field seprator as space till colon and then space 
'NF>1{                                            ####### If Number of fields are greater than 1  
sub(/^[[:space:]]+/,X,$0);                        ####### Now subsituting the initial space of each line to NULL.
for(i=1;i<=NF;i++){                               ####### Now going to each field of line by for loop.
Q=Q?Q OFS "<td>" $i "</td>":"<td>" $i "</td>"};   ####### NOw creating a variable named Q whose value is equal to "<td>" value of field($i) "</td>" first time then it will keep append value to variable itself which is Q till number of fields are completed for that particular line.
print "<tr>" Q "</tr>";                           ####### Now printing the strings "<tr>" then value of variable Q "</tr>"
Q="";                                             ####### Nullifying the value of Q as next line it should not append previous values of Q.
next}                                             ####### Now using next statment of awk and skipping all further statments now.
NF==1{                                            ####### When number of fields are 1.
sub(/^[[:space:]]+/,X,$0);                        ####### When above condition is TRUE then substitute initial space of each line to NULL.
print "<tr><td>"$0"</td></tr>"}'                  ####### Now printing strings "<tr><td>" value of line "</td>"</tr>".
OFS="" Input_file                                 ####### Setting output field seprator to NULL now and mentioning Input_file.

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 01-11-2016
Hello Ravinder ,

How can be have the <tr> and <td> tags around a space separated file. For example below is the file :
Code:
Ide  Intir TID Grp  Vendor     Type       Hypr      Total       Free
MH-1E    C   4    4 SFGRET     MED7600      44    1823565      36236
Totals                                                          1823565      36235

expected Output is like below. Also the last line has only first field and the last 2 fields.

Code:
<tr><td>Ide</td><td>Intir<td><td>TID</td><td>Grp</td><td>Vendor</td><td>Type</td><td>Hypr</td><td>Total</td><td>Free</td></tr>
<tr><td>MH-1E</td><td>C</td><td>4</td><td>4</td><td>SFGRET</td><td>MED7600</td><td>44</td><td>1823565</td><td>36236</td></tr>
<tr><td>Totals</td><td></td><td></td><td></td><td></td><td></td><td></td><td>1823565</td><td>36235</td></tr>

# 7  
Old 01-11-2016
Hello rahul2662,

Following may help you in same.
Code:
awk 'NR==1{n=NF;}{if(n==NF){for(i=1;i<=n;i++){A=A?A OFS "<td>"$i"</td>":"<td>"$i"</td>"};print "<tr>"A"</tr>";A=""}} {if(NF<n){for(j=2;j<n-2;j++){B=B?B OFS "<td></td>":"<td></td>"};print "<tr><td>" $1 "</td>" B "<td>" $(NF-1) "</td><td>" $(NF) "</td></tr>"}}' OFS=""   Input_file

Output will be as follows.
Code:
<tr><td>Ide</td><td>Intir</td><td>TID</td><td>Grp</td><td>Vendor</td><td>Type</td><td>Hypr</td><td>Total</td><td>Free</td></tr>
<tr><td>MH-1E</td><td>C</td><td>4</td><td>4</td><td>SFGRET</td><td>MED7600</td><td>44</td><td>1823565</td><td>36236</td></tr>
<tr><td>Totals</td><td></td><td></td><td></td><td></td><td></td><td>1823565</td><td>36235</td></tr>

Here things which we should consider is on first line this code will record the number of fields and then each line it will look for same number of fields.
Also lines which are lesser number of fields then it will always fill strings <td></td> till $(NF-1) fields because you have mentioned only last 2 fields will be there. Could you please check it and let me know if this helps you.

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-11-2016 at 10:41 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert csv data to html format

I am new to html and need to convert the attached csv file data to html format ; running into issues. please assist. #!/bin/ksh echo "<html>" ; echo "<head><style> table {border-collapse: collapse;} table, td, th {border: 1px solid black;} </style></head>" echo "<title> REPORT </title>" echo... (0 Replies)
Discussion started by: archana25
0 Replies

2. Shell Programming and Scripting

Convert shell script output txt file to html table

My concnern related to the post -Convert shell script output txt file to html table, in this how to print the heading as color. awk 'BEGIN{print "<table>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table>"}' <filename> (8 Replies)
Discussion started by: sarajobmai
8 Replies

3. Shell Programming and Scripting

Format text file to html

Hi Experts, Anybody out there figure out on how to achieve in shell scripts or tools. I have done googling to find solutions but no luck. I have thousands of .txt files to batch process, please see the below sample text content after -------- start here --------. What I want to achieve is to... (10 Replies)
Discussion started by: lxdorney
10 Replies

4. Shell Programming and Scripting

Convert text file to HTML tabular format.

Please provide script/commands to convert text file to HTML tabular format. No need of styles and colours, just output and a heading in table is required. Output file will be send via email and will be seen from outlook. (script required without using awk). output file content: (sar... (7 Replies)
Discussion started by: Veera_V
7 Replies

5. Shell Programming and Scripting

Html output in correct format

Hi, I am running two scripts as below. In Script 1 i am getting correct output in proper HTML format while in script 2 i am not getting output in mail and only html code is getting printed.I want to get the output of script 2. Please guide. 1.IFILE=/home/home01/Report.csv if #Checks... (7 Replies)
Discussion started by: Vivekit82
7 Replies

6. Shell Programming and Scripting

Need to convert output.txt into html file

I have output.txt file generated through shell scripts which need convert in tabular format using html can you please help me output.txt Token State Date1 Date2 Description Name 34567 open 27/06/13 28/06/13 ... (5 Replies)
Discussion started by: vijay_rajni
5 Replies

7. Shell Programming and Scripting

Convert TZ output to a different format

Friends, I am trying to convert my local server timezone EST to UTC and for which I used the TZ command, see below $ date Thu Dec 6 10:14:14 EST 2012 $ $ TZ=UTC date -d '10:14 EST' Thu Dec 6 15:14:00 UTC 2012 Now I would like to have the same output in 'yyyymmdd hh:mm' format. ... (4 Replies)
Discussion started by: vivek_damodaran
4 Replies

8. Shell Programming and Scripting

Convert shell script output txt file to html table

Hi, I have script which generates the output as below: Jobname Date Time Status abc 12/9/11 17:00 Completed xyz 13/9/11 21:00 Running I have the output as a text file. I need to convert it into a HTML Table and sent it thru email ... (6 Replies)
Discussion started by: a12ka4
6 Replies

9. Shell Programming and Scripting

Is it possible to convert text file to html table using perl

Hi, I have a text file say file1 having data like ABC c:/hm/new1 Dir DEF d:/ner/d sd ...... So i want to make a table from this text file, is it possible to do it using perl. Thanks in advance Sarbjit (1 Reply)
Discussion started by: sarbjit
1 Replies

10. Shell Programming and Scripting

Convert comma text file to Column in html format

I am trying to generate a report with below file : File1 : EQADM,edrtere9-phys,8122caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere11-phys,8227caef0,gpatmon,/bin/ksh,nuten Erick EQADM,edrtere3-phys,822caef0,gpatmon,/bin/ksh,nuten Erick can you help me convert it to html and add... (9 Replies)
Discussion started by: sriram003
9 Replies
Login or Register to Ask a Question