htmlbashshellawkaix

How to convert txt into html format using shell/bash


I have below data in a text file:

Details_A
name: A1
Valid: A1_Value
name: A2
Valid: A2_Value
Details_A2
name: A2
Valid: A2_Value
name: A2
Valid: A2_Value

which I am trying to convert into in below html table:

Details

Output HTML Table


Solution

  • You can use awk like this :

    awk 'BEGIN { 
            x = 0;
            print "<table border="1">"
        }
        {
            if (NF == 1){
                print "<tr ><td colspan="2">"$i"</td>";
                print "</tr>"
            } else {
                if (x == 0){
                    x++;
                    print "<tr><td>"$i"</td>"
                } else {
                    x = 0;
                    print "<td>"$i"</td></tr>"
                }
            }
        }
        END {
            print "</table>"
        }' input.txt > table.html
    

    Feel free to add any additional style

    For older version of awk, you can use the following, tested on an awk implementation of 2009-11-26 (from one-true-awk) :

    awk  'BEGIN {
            x = 0;
            y = 0;
            print "<table border="1">"
        }
        {
            for (i = 1; i<=NF ; i++){
    
                if (NF == 1){
                    print "<tr ><td colspan="2">"$i"</td></tr>";
                } else {
    
                    if (x == 0 && y == 0){
                        print "<tr><td>"$i" ";
                        x++;
                    }
                    else if (x == 0 && y == 1){
                        print "<td>"$i" ";
                        x++;
                    }
                    else if (x==(NF-1)){
                        x = 0;
                        y++;
                        if (y == 2){
                            y = 0;
                            print ""$i"</td></tr>";
                        }
                        else{
                            print ""$i"</td>";
                        }
                    }
                    else {
                        print ""$i" ";
                        x++;
                    }
                }
            }
        }
        END {
            print "</table>"
        }' input.txt > table.html
    

    For this last version, x is incremented at each space delimiter until we reach NF-1 which is the last word and we should put an ending </td>. The decision for the ending </tr> depends on the value of y which is incremented at each line and re-initialized when the max count of <td> is reached (here 2 <td> per <tr>)