5

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>"
2
  • Welcome to Stack Overflow! It's good that your question includes a small example of input and output. It would be even better if the question also showed what you have tried so far -- that is, if you showed your research.
    – RJHunter
    Commented Nov 2, 2016 at 2:21
  • @RJHunter updated Commented Nov 2, 2016 at 2:35

2 Answers 2

5

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>"
}
3

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.

2
  • This is golfed so as to be cryptic. It looks like it could actually be very elegant, but your use of the ternary operator along with the equality comparison and assignment all in the same line means that even advanced Awk users will have to consult the documentation to be sure just exactly how that line will be parsed.
    – Wildcard
    Commented Apr 25, 2019 at 22:52
  • Perhaps add parenthesis around equality test? Not sure what else is hard to understand. It's not golfed (I do that sometimes) but written tersely.
    – karakfa
    Commented Apr 26, 2019 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.