Home > Software engineering >  Create a table with style from file
Create a table with style from file

Time:05-17

I am trying to create an table from a csv file. In the csv file I have the three fields that are already filtered so that it does not generate problems, but when I run the code, the report file does not generate any output and it must be an error when going through the file or I do not know where else is the failure:

The csv input looks similar to this:

CodePudding user response:

I cannot see an obvious error in your file but was able to generate the required html file using an awk script file as follows (the correct #! path can be found using which awk in terminal):

#! /usr/bin/awk -f


BEGIN {
FS =","
print "<!DOCTYPE html>\n<head><title>Report</title>"
print "<link rel=\"stylesheet\" href=\"style.css\">"
print "<meta charset=\"utf-8\"/>" 
print "</head>\n<body>\n<div class=\"head-style\"<h2>Report</h2>\n</div>"

}

NR<2 {
print "<table>\n<tr><th>"$1"</th><th>"$2"</th><th>"$3"</th>"
}

NR>1 {
print "<tr><td>"$1"</td><td>"$2"</td><td>"$3"</td></tr>"
}

END {
print "</table>\n<div class=\"footer\">\n<p>0000 st</p>\n</div>\n</body>\n</html>"
}

I formatted the printing using field references $1 etc. between quotted string. Note also, quotes can be escaped for printing.

I saved the script as awkScript.awk and made it executable from the command line using:

chmod  x awkScript.awk

This can then be executed on the csv file with the command:

./awkScript.awk rm.csv > rep.html

CodePudding user response:

It looks like you forgot to pass the fields as parameters to function print_line(Platan, Recl, Tror).

# Since "Platan" and "Recl" are strings, I think the format string
# should be: "%s %s %.2f %s\n" (BTW, I included "\n" to improve readability).

function print_line(Platan, Recl, Tror) { 
  printf("%s %s %.2f %s\n", "<tr><td>"Platan"</td>", "<td>"Recl"</td><td>", Tror, "</td></tr>") ;
}

{
  if (NR > 1) {
    print_line($1, $2, $3)   # should solve
  }
}
  • Related