cyclONEnation
cyclONEnation

Reputation: 51

AWK to convert CSV to HTML Table

Just messing arround in linux and dabaling into AWK. How would I go about changing a CSV formatted file into a HTML formatted one. For example... here is the information I have loaded into my shell...

user$ cat table.csv
Ep#,Featured Film,Air date
211,First Spaceship on Venus,12/29/90
310,Fugitive Alien,08/17/91
424,Manos: The Hands of Fate,01/30/93

Then after running the code this is what should be outputted.

user$ csv2html.awk table.csv
<html><body><table>
<tr>
<th>Ep#</th>
<th>Featured Film</th>
<th>Air date</th>
</tr>
<tr>
<td>211</td>
<td>First Spaceship on Venus</td>
<td>12/29/90</td>
</tr>
<tr>
<td>310</td>
<td>Fugitive Alien</td>
<td>08/17/91</td>
</tr>
<tr>
<td>424</td>
<td>Manos: The Hands of Fate</td>
<td>01/30/93</td>
</tr>
</table></body></html>

I have tried some things along the line of this but I am having some complie errors...

#!/bin/awk
print "<tr>
for( i = 1; i <= NF; i++)
     print "<td> "$i" </td"
#print "</tr>"

Upvotes: 5

Views: 8422

Answers (2)

karakfa
karakfa

Reputation: 67567

another similar awk,

    BEGIN{header = "<html><body><table>"; print header}
         {c = NR == 1 ? "th" : "td";
          OFS = et(c) bt(c);
          $1 = $1;
          print wrap("tr", wrap(c,$0)) }
      END{gsub("<","</",header); print header }

    function wrap(t, v) { return bt(t) v et(t)}
    function bt(t) {return "<" t ">"}
    function et(t) {return "</" t ">"}

instead of looping elements uses OFS to insert the corresponding xml tags.

Upvotes: 3

Marcus Tuke
Marcus Tuke

Reputation: 86

There are many ways to do this in AWK but my preferred way is the code below. I've included some explanations as comments in the code. Hope this helps!

To run on the CLI, Save the code in a file such 'csv_to_html.awk' and execute with 'table.csv' as an argument:

$ chmod +x csv_to_html.awk
$ ./csv_to_html.awk table.csv > table.html

Code:

#!/bin/awk -f

# Set field separator as comma for csv and print the HTML header line
BEGIN {
    FS=",";
    print "<html><body><table>"
}
# Function to print a row with one argument to handle either a 'th' tag or 'td' tag
function printRow(tag) {
    print "<tr>";
    for(i=1; i<=NF; i++) print "<"tag">"$i"</"tag">";
    print "</tr>"
}
# If CSV file line number (NR variable) is 1, call printRow fucntion with 'th' as argument
NR==1 {
    printRow("th")
}
# If CSV file line number (NR variable) is greater than 1, call printRow fucntion with 'td' as argument
NR>1 {
    printRow("td")
}
# Print HTML footer
END {
    print "</table></body></html>"
}

Upvotes: 5

Related Questions