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
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 :
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
while read line
do
read -a arr IFS=':'
count=`echo "$line" | awk -F":" '{print NF}'`
echo $count
done < raw.txt
Please help
<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>
Following is the explanation for same which may help you, let me know if you have any queries on same.
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.
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.